Skip to content Skip to sidebar Skip to footer

Javascript: Changing An Image On Click And Then Back To Original When Clicked Again

Hello I have been looking around and I can't seem to find what I'm looking for. I have tried a few solutions suggested on other posts which have not worked in my favor. The title p

Solution 1:

If you are using jQuery, this is simple example, you can replace the background color to image

$( "div" ).click(function() {
  $( this ).toggleClass( "change" );
});
#box{
  background-color:red;
  width:300px;
  height:300px;
}

#box.change{
  background-color:green;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="box"class=""></div>

Solution 2:

There is no need for all that code. All you need to two CSS rules and to toggle a class name. The one CSS rule has the one image and the other rule has the other. You add the class and the second image appears.

var btns = document.getElementsByTagName("button");
for (var i = 0; i < btns.length; i++) {
  btns[i].addEventListener("click", function() {
    document.getElementById(this.dataset.for).classList.toggle("active");
  });
}
div.x {
  height: 100px;
  width:100px;
  background-image:url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg);
}
div.x.active {
  background-image: url(http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg);
}
<divclass="x"id="demo"></div><buttondata-for="demo">Run</button><divclass="x"id="demo2"></div><buttondata-for="demo2">Run</button>

Solution 3:

The first function doesn't set the background image. So it's not surprising it isn't changing. The second function checks if it's blue and if so, sets it to blue. The second function doesn't change it, it simply confirms existing state.

if (document.getElementById("demoNoVars").style.backgroundImage == "url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg)") {
    document.getElementById("demoNoVars").style.backgroundImage = "url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg)";

has the same url so nothing changes.

You should be swapping your if conditions in the second function and the first function should be setting the background.

Solution 4:

Here's a really simple script, I've used a data-attribute to store you second image, I grab these into an array and rotate.

var imgs = document.querySelectorAll('img');

[].slice.apply(document.querySelectorAll('img')).forEach(function (f) {
  var im = [ f.getAttribute("src"), f.getAttribute("data-src2") ];
  f.onclick = function () {
    var k = im.shift(1); im.push(k);
    f.setAttribute("src", im[0]);
  };
});
<imgsrc="http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg"data-src2="http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg"><imgsrc="http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg"data-src2="http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg"><imgsrc="http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg"data-src2="http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg">

Solution 5:

I had a few issues when moving some of these answers to my environment, but using them to do more research I found something that worked for me.

I want to post the solution here just incase someone comes across this question and the other solutions didn't work in their environments either.

Here is the code:

functionmyFunction() {
    var x = document.getElementById("demo");
    if (x.className.indexOf("red") == -1) {
        x.className = x.className.replace("blue", " red");
    } else {
        x.className = x.className.replace("red", " blue");
    }
}
.blue {
    height: 100px;
    width:100px;
    background-image:url(http://www.babybedding.com/images/fabric/solid-turquoise-fabric_smaller.jpg);
}
.red {
    height: 100px;
    width:100px;
    background-image: url(http://www.babybedding.com/images/fabric/solid-coral-fabric_smaller.jpg);
}
<divid="demo"class="blue"></div><buttononclick="myFunction()" >Button</button>

Thanks everyone for the responses!

Post a Comment for "Javascript: Changing An Image On Click And Then Back To Original When Clicked Again"