I want to move an image on each click but using this code works only on one click.
function moveright() {
console.log("moveright invoked.");
var myImg = document.getElementById("image");
myImg.style.position = "absolute";
var cLeft = myImg.style.left;
if (cLeft == 500) {
console("Maximum reached.");
} else {
myImg.style.left = (cLeft + 50) + "px";
}
}
<img id="image" src="https://i.sstatic.net/TiYE3.jpg?s=32&g=1" onclick="moveright()" />
console.log(cLeft + 50);to see what you get. It is not doing what you think it is.