Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

onclick change image src 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 :: axios get data 
Javascript :: How to access return value of promise 
Javascript :: python json.dumps pretty print 
Javascript :: flatten an array without using .flat(); 
Javascript :: Error: Not implemented: window.alert 
Javascript :: typeof 
Javascript :: LF would be replaced by CRLF in package-lock.json 
Javascript :: jquery set hidden field value 
Javascript :: how to open component as a dialog in angular 
Javascript :: nestjs vscode debug 
Javascript :: this.props.history.location.push 
Javascript :: react pagination 
Javascript :: javascript take last element of array 
Javascript :: unsplash 
Javascript :: sum all the values in an array javascript 
Javascript :: webpack sass 
Javascript :: how to check localstorage not set 
Javascript :: how to contain image size 
Javascript :: for in loop javascript 
Javascript :: react native navigation navigate 
Javascript :: js loop over array of objects extract value 
Javascript :: react cheat sheet 
Javascript :: firebase where or 
Javascript :: js pad 2 
Javascript :: node js async delay 
Javascript :: saving text in javascript 
Javascript :: import img react in another file 
Javascript :: change title react 
Javascript :: check css property jquery 
Javascript :: change port react app 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =