dutchkillsg
dutchkillsg

Reputation: 373

Confused on how to use values in an array with another variable

I have a tic tac toe game with the variable player = 1. This variable toggles to player = 3-player to simulate turns in the game.

I added a form in my html to get player names and using jquery I pulled the input values of the forms and pushed them into an empty names array in my global scope.

$(document).ready(function(){
    var names = []
    var player = 1;
    // Define the classes that are used for the two symbols
    var symbols = ['fa-times', 'fa-circle-o'];
    // Get collection of the 9 squares
    var $squares = $(".square");
    // Set the filter to be used to identify occupied square(s)
    var occupied = "." + symbols.join(", .");

    $("#namesForm").submit(function(){
       names.push($("#player1").val())
       names.push($("#player2").val())

    }); // cant get the player names to work with the player variable. 

The problem is that since my player variable is changing the turns for the game I cant seem to figure out what way to implement the player naming system.

I fixed the form to submit, before I had the actual button submitting but I am still getting the same result when I try various options

I have a working jsfiddle if anyone wants to see how that player variable is implemented tictactoe

Upvotes: 0

Views: 50

Answers (1)

Wayne Allen
Wayne Allen

Reputation: 1745

The JQuery submit event is fired on the target form, not the submit button. If the submit method is not handled on the form, or does not return false, the page will reload.

I have created this fiddle to demonstrate how you should use submit. This way, your names array is correctly populated.

Upvotes: 1

Related Questions