I'm new to JavaScript, and I'm trying to alternate an image every click from 1st to 2nd and from 2nd to 1st(indefinitely). I tried this:
function change() {
var imgId = document.getElementById("image");
var ubuntu = imgId.src="ubuntu.jpg";
var debian = imgId.src="debian.jpg"
if(imgId.src="ubuntu.jpg") {
return debian;
} else if(debian) {
return ubuntu;
}
}
<img id="image" src="ubuntu.jpg" width="160" height="120" onclick = "change()">
Also, I would like to know the reason why the code is changing the image from ubuntu to debian but not from debian to ubuntu.
==
or===
. You're using=
(assignment) which is messing up the logic. – JJJ Jun 14 '15 at 13:02