Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67497

JavaScript: Changing image in HTML with button

$
0
0

I have a problem with my code where I change an image by clicking a button in HTML, but when I try to run the code in the console it says it my image.src is undefined.

This is my code in javaScript

var image = document.getElementById("sider").src="side1.jpg";
var image_tracker = "1"

function changeImage() {
  if(image_tracker == "1") {
    image.src="side2.jpg";
    image_tracker = "2";
  }
  else image.src="side1.jpg";
}

And this is my HTML

<p>
<img id="sider" src="side1.jpg" style="width:450px;height:650px;">
</p>


<center> 

<button type="button" onclick="changeImageBack()"> < </button>
<button type="button" onclick="changeImage()"> > </button>```

I haven't made the function for ```changeImageBack()``` yet, so ignore that.

Viewing all articles
Browse latest Browse all 67497

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>