user2619857 user2619857 - 1 year ago 46
Javascript Question

Global boolean not working within function - Javascript

var xTurn = true;
function changeImage()
if (element.src.match("/wp-content/uploads/2013/07/squaree.png") && xTurn)
xTurn = false;
else if (element.src.match("/wp-content/uploads/2013/07/squaree.png") && xTurn==false);
xTurn = true;

<img id="myimage" onclick="changeImage()" src="/wp-content/uploads/2013/07/squaree.png" width="90" height="90">

For some reason xTurn doesn't seem to work within this function. Am I referencing if incorrectly? This is in wordpress, by the way. I have already checked to make sure the image changes without xTurn being used.

Answer Source

I think you are using match wrong and element.src will give you the complete url with


try something like

element.src.indexOf("squaree") != -1