Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

onclick change image javascript example

//in html
<img id="img" data-image="user1" src="/assets/user1.jpg" onclick="change_image()" />

//in javascript
const img = document.getElementById('img');
const change_image = function() {
  if (img.dataset.image == "user1") {
  	img.src = "/assets/user2.jpg";
    img.dataset.image = "user2";
    return
  };
  
  if (img.dataset.image == "user2") {
  	img.src = "/assets/user1.jpg";
    img.dataset.image = "user1";
    return
  };
};
Comment

js click Change img

function imagefun() {
            var Image_Id = document.getElementById('getImage');
            if (Image_Id.src.match("imageName1.jpg")) {
                Image_Id.src = "imageName2.jpg";
                Image_Id.src = "imageName3.jpg";
            }
            else {
                Image_Id.src = "imageName1.jpg";
            }
        }
Comment

change image on click javascript

function changeImage() {
    if (document.getElementById("imgClickAndChange").src == "http://www.userinterfaceicons.com/80x80/minimize.png"){
        document.getElementById("imgClickAndChange").src = "http://www.userinterfaceicons.com/80x80/maximize.png";
    } else {
        document.getElementById("imgClickAndChange").src = "http://www.userinterfaceicons.com/80x80/minimize.png";
    }
}
Comment

change image onclick js

<p>
    <img alt="" src="http://www.userinterfaceicons.com/80x80/minimize.png" 
        style="height: 85px; width: 198px" id="imgClickAndChange" onclick="changeImage()"/>
</p>
<script>
function changeImage() {
    if (document.getElementById("imgClickAndChange").src == "http://www.userinterfaceicons.com/80x80/minimize.png"){
        document.getElementById("imgClickAndChange").src = "http://www.userinterfaceicons.com/80x80/maximize.png";
    } else {
        document.getElementById("imgClickAndChange").src = "http://www.userinterfaceicons.com/80x80/minimize.png";
    }
}
</script>
Comment

PREVIOUS NEXT
Code Example
Javascript :: mongoose updateone example 
Javascript :: hex string to decimal string javascript 
Javascript :: async await iife 
Javascript :: flatten nested object js 
Javascript :: how to convert string to uppercase in javascript 
Javascript :: make react project 
Javascript :: how to remove last element of array in javascript 
Javascript :: find average of numbers 
Javascript :: chrome storage local example 
Javascript :: change class of icon using jquery 
Javascript :: split string every nth characters javascript 
Javascript :: cors error in node js express 
Javascript :: jquery class selector 
Javascript :: update angular project 
Javascript :: react native margin vs padding 
Javascript :: hello world in javascript 
Javascript :: disable long press on chrome 
Javascript :: dm message collector discordjs 
Javascript :: how to set window location search without reload 
Javascript :: link tag react 
Javascript :: make multiple api call using promise.all 
Javascript :: get duplicate value javascript 
Javascript :: js logical operators 
Javascript :: operators in js 
Javascript :: javascript add to object 
Javascript :: Alpinejs notification 
Javascript :: angularjs 1.5.6 cdn 
Javascript :: find all in array javascript 
Javascript :: get static props 
Javascript :: javscript ajax request gmt header 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =