I have broken the 9 potential spaces into a grid labeled A1, A2, A3, B1… ect.
What I’ve done so far is when a user clicks on a space their piece shows up. But what I need to do next is also have the appropriate variable = the user’s piece. I’m not sure how to do this.
My div id’s are named the same as the corresponding variables. So, if a user clicks on div A1, their letter shows up. Next variable A1 should = X or O depending on what they chose. How could I do this?!
Perhaps you can help with another part of my code. I am trying to make the validation to prevent a user from selecting a square that has been selected already. I wrote:
$(".gameSquare").click(function (evt) {
if (playerTurn) {
if ($("h2", this).html !== "") {
alert("Pick an empty square") //HELP WITH THIS VALIDATION
}
But the problem is the alert comes up no matter what. How can I test to see if it is just empty html or not?
You are almost there and are trying to check whether the h2 cell is empty.
Rather then using ‘this’, use the evt object from the parameter. Let say you wanted to get to h2 in A1, from a CSS point of view you would use #A1>h2. Then use the html() function to get the content and check that it is empty.
Let’s write this in Javascript terms and change the condition statement to:
if ( $("#"+evt.target.id+" > h2").html() !== "") { ....your code goes here }
For your test button, you left off the brackets when calling the html function to retrieve the data stored between the tags. Use alert( $("#A1 > h2").html() );
Make the same change to if ($("#" + evt.target.id + " > h2").html() !== "")
Let me know if this helps.
I’m not sure what’s happened but this I’ve got this sort of working-- You aren’t able to select a chosen square, however if you do the alert message doesn’t come up. It’s not a very big deal, I’m just not sure why that has occurred.
I was wrong, it does work. I was just clicking on the text in the square when I tested. But if I click the outer edges of the square the alert comes up. Thanks for all the help!