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 :: javascript log html element as dom object 
Javascript :: javascript how to increment by other than one in for loop 
Javascript :: jquery upload image 
Javascript :: nuxt emit 
Javascript :: Material-ui add alert icon 
Javascript :: flask return status code 200 and json 
Javascript :: object deep copy 
Javascript :: Reverse a String With Built-In Functions 
Javascript :: react text input onchange 
Javascript :: javascript play pause button 
Javascript :: jquery attr 
Javascript :: react public pic 
Javascript :: how to run function after animation complete jquery 
Javascript :: foreach javascript arrow function 
Javascript :: how to check value is array or not in javascript 
Javascript :: Error: Unable to resolve module ./index from 
Javascript :: handlerbar console log 
Javascript :: bootstrap datepicker set min and max date 
Javascript :: jquery change tabs 
Javascript :: javascript canvas beziercurveto 
Javascript :: redis nodejs 
Javascript :: checkbox set checked jquery 
Javascript :: Set up routes for vue in laravel 
Javascript :: convert string to set in js 
Javascript :: join a list of strings into one string javascript 
Javascript :: javascript foreach example 
Javascript :: js arithmetic operators 
Javascript :: Simple code example of adding two numbers in javascript 
Javascript :: create a binary tree 
Javascript :: how to assign value to variable 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =