Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

change image src 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

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 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 :: cancel axios request 
Javascript :: json array 
Javascript :: expo react native send image to api 
Javascript :: ajax onchange dropdown 
Javascript :: avoid no-param-reassign when setting a property 
Javascript :: open bootstrap modal with javascript 
Javascript :: javascript random number generator 
Javascript :: javascript date convert to unix 
Javascript :: preload javascript 
Javascript :: javascript right trim 
Javascript :: detect resize window javascript 
Javascript :: reset input react 
Javascript :: rounding number to x decimals javascript 
Javascript :: smooth scroll mouse wheel javascript 
Javascript :: how to find last element of array react 
Javascript :: uncaught typeerror is not a function javascript 
Javascript :: split string based on length in javascript 
Javascript :: renemane object key js 
Javascript :: how to change materil ui divider coloer 
Javascript :: convert date to string javascript 
Javascript :: is a letter javascript 
Javascript :: json vs gson 
Javascript :: javascript get date 
Javascript :: jquery insert after next element 
Javascript :: res.locals in express 
Javascript :: react join array of components 
Javascript :: get object with max value javascript 
Javascript :: upload files to express using express-fileupload 
Javascript :: vue fix eslint error 
Javascript :: object destructuring javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =