I'm just learning to use JavaScript, and at the moment I'm working on writing a program to play noughts-and-crosses. It will use a grid of html buttons within a form (name/id="board") as the playing board. The onclick property of each of these buttons calls a function called
Click, passing its element number within the form (held in the variable
btn).
I was playing around with controlling the value of these buttons, depending on their existing value. I tried using the following seies of
if statements, but it does not work (the first option is always executed - setting the button's value to
"X"):
Code:
var checkit = document.forms["board"].elements[btn].value
if(checkit=" ")
{document.forms["board"].elements[btn].value="X";}
else if(checkit="X")
{document.forms["board"].elements[btn].value="0";}
else if(checkit="0")
{document.forms["board"].elements[btn].value=" ";}
However, the equivalent
switch statement works perfectly, allowing me to cycle through the 3 values:
Code:
switch(document.forms["board"].elements[btn].value)
{
case " ":
document.forms["board"].elements[btn].value="X";
break;
case "X":
document.forms["board"].elements[btn].value="0";
break;
case "0":
document.forms["board"].elements[btn].value=" ";
break;
}
Can anyone see what I may be doing wrong? Or is it a known problem?
I'm using SuSE 10.0 with Gnome 2.12, and I've tested this in both Firefox 1.5 and Konqueror 3.4.2.
Thanks,
Rob