Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

change image onclick javascript

//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

change image onclick javascript

<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 :: merge objects javascript 
Javascript :: node how to stop NodeJS server process 
Javascript :: ajax upload image 
Javascript :: iterate over array of objects javascript 
Javascript :: absolute value array javascript 
Javascript :: jquery get by name 
Javascript :: javascript radio button value if checked 
Javascript :: reverse words in a string javascript 
Javascript :: custom event js 
Javascript :: javascript format date 
Javascript :: nodejs path 
Javascript :: validate password with 8 Characters, One Uppercase, One Lowercase, One Number and One Special Case Character 
Javascript :: js read text file line by line 
Javascript :: javascript long integer 
Javascript :: get an array with unique values 
Javascript :: what is computed in mobx 
Javascript :: javascript enable clipboard 
Javascript :: js copy to clipboard 
Javascript :: create a form and submit it dynamically jquery 
Javascript :: show hidden element javascript 
Javascript :: how to push array into array in angular 
Javascript :: how to manage a db connection in javascript 
Javascript :: inheritance in es6 
Javascript :: How to Use the toLowerCase() String Method in javascript 
Javascript :: javascript join list of string 
Javascript :: shuffle array item javascruitp 
Javascript :: node js download file to folder 
Javascript :: javascript getting input from console 
Javascript :: square node js 
Javascript :: return last two values of array in javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =