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 :: delete all the fields on the form whit jquery 
Javascript :: regex e-mail 
Javascript :: image in react jsx 
Javascript :: convert date dd/mm/yyyy to date object js 
Javascript :: vowels Count js 
Javascript :: jquery get cursor position 
Javascript :: how to flat an array in javascript iteratively 
Javascript :: javascript array split empty string 
Javascript :: export data in json format in javascript 
Javascript :: javascript array to string remove comma 
Javascript :: extract value from object javascript 
Javascript :: angular 6 key value pair getvalue example 
Javascript :: Javascript How to push a key value pair into a nested object array 
Javascript :: react native pm ERR! code EINTEGRITY 
Javascript :: angular return observable with error 
Javascript :: foreach 
Javascript :: array put value in index 
Javascript :: js returns the number of true values there are in an array 
Javascript :: api.fetch saga 
Javascript :: A bad HTTP response code (404) was received when fetching the script. 
Javascript :: catch javascript 
Javascript :: javascript round to 7 decimal places 
Javascript :: delete space from string javascript 
Javascript :: redux dev tool 
Javascript :: get current store id magento 2 
Javascript :: JavaScript Split the string into an array of characters 
Javascript :: how to access css and js with nodejs 
Javascript :: angular timeout function 
Javascript :: for each 
Javascript :: the sum of all first n natural numbers js 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =