I'm working on a project where I need to read a form, then create objects from the form, which can then be used to keep track of players in the game, and their score.
This is the constructor:
function Player(x) {
this.playerName = x;
this.playerScore = 0;
}
Here's the form:
<form id="PlayerName">
<label for="playerName1"><h3>Player 1, please input your name:</h3></label>
<input type="text" id="playerName1"></input><br>
<button type="submit" name="playerName">Submit Name</button>
</form>
Here's the JS/jQuery:
$("form#PlayerName").submit(function(event){
event.preventDefault();
var inputName1 = $("input#playerName1").val();
var player1 = new Player(inputName1);
});
When I run the debugger, it finds the object and looks like it's creating it, with the name in the proper spot then when I try to run the following function against that object, it says that it's not found, and I'm totally stumped:
$("form#HoldDice").submit(function(event) {
event.preventDefault();
HoldTheDice();
if (currentPlayer !== 1) {
$("span#PlayerOneScore").text(player1.playerScore);
$("span#DiceRoll").text("Player One, you've chosen to hold the dice, you added " + addscore + " to your score.")
addscore = 0;
}...
currentPlayer is a variable used to keep track of which players turn it is, I don't think it's relevant to this question though.
Help?
submitevents since you not really submitting data anywhere and you are cancelling the event in the first place and instead use regular buttons and work with theclickevent of those. Next, you need to give us more context for your question. Where iscurrentPlayerdefined? And, what exact line of code causes the not found issue?click, butsubmitis a very specific event for gathering up all yourformdata and transmitting it back to the server. Since you're not doing that (and in fact, you are cancelling that event withevent.preventDefault()),submitis the wrong event. Since you want it to happen when someone clicks yourbutton, change thebuttonto be just a regularbutton(not asubmit) and handle theclickevent. Then, you'll be able to omit theevent.preventDefault();.