Having trouble shuffling picture src using javascript

Having trouble shuffling picture src using javascript
0.0 0

#1

I watched a tutorial by this guy - Quentin Watt where he changes an image source whenever he clicks the picture.
There is a picture on the screen when the page opens. Then he clicks the picture and it switches to another picture,replacing the first one. Then he clicks the other one - and it goes back to the first one. And so on…
I have done, in my opinion, the exact same thing,just with different pictures than the ones Quentin used in his tutorial.
But it only works the first time I click it.
The picture changes to the second one. But when I click the second (new) one - nothing happens. There must be something i am not noticing about my code…

I should be using only JavaScript and HTML in order to do this.

Here is my code… Could someone please help?

<head>
<meta charset ="UTF-8" />
<title>The Trick With The Pictures</title>


<script type="text/javascript">

var image_tracker ="h";

function changeit() {
	var IMAGE = document.getElementById("hs");
	if (image_tracker == "h") {
	IMAGE.src = "Sad-smiley.jpg";
	image_tracker == "s";
		}
	else {
	IMAGE.src = "Happy-smiley.jpg";
	image_tracker == "h";
}
}
</script>
</head>
<body>
<p>
<img id="hs" onclick="changeit();" width="300px;" src="Happy-smiley.jpg" >
</p>

</body>
</html>

#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


#3

If you want to set the value of image_tracker to "s", you should use a single =:

image_tracker = "s";

#4

Exactly ! Thanks :smiley: