user2619857 user2619857 - 5 months ago 9
Javascript Question

Global boolean not working within function - Javascript

<script>
var xTurn = true;
function changeImage()
{
element=document.getElementById('myimage')
if (element.src.match("/wp-content/uploads/2013/07/squaree.png") && xTurn)
{
xTurn = false;
element.src="/wp-content/uploads/2013/07/xx.png";
}
else if (element.src.match("/wp-content/uploads/2013/07/squaree.png") && xTurn==false);
{
xTurn = true;
element.src="/wp-content/uploads/2013/07/circ.png";
}
}
</script>

<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

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

http://domain....

try something like

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