I created a page that will take a users input and then compare the input with a word. The idea is that the user has 6 chances to guess the word. If the user guesses the word or ran out of guesses the button clicked will change from Guess to Start a New Game. All that I got right.

The idea is then that the user will click the Start a Game button and the page will reset to original values, here is where I am having a problem. My button will change to the new value and the new id, but when I click the button the game still plays and then when I click it again and it does reset. Why is it not resting at the first click?

Here is how the part of the function looks

else if (theGuess.toLowerCase() == magicWord.toLowerCase()) 
    //The user guesses the correct word
    $("input[type=text]").ready(function () { $("#guestGuess").addClass("unblur"); });
    $("input[type=submit]").val("Start a New Game");
$('#startNew').click(function() {
magicWordResult = "Your guess was " + theGuess + "! Nice going!";
//document.writeln("Your guess was: " + guestGuess + "\nand the magic word is " + magicWord);

You can find the whole code here http://liveweave.com/psAsxh , if you run it you can see what is my problem.

Recommended Answers

All 3 Replies

I could only try it once before liveweave failed to reload it but the most obvious problem was that the validation check on the button still ran, checkig to see if I had added a value into the textbox. You'll want to disable that when the text of the button is 'start new game'.

Member Avatar

Hi gcardonav,

You could try using window.location.reload instead. If you are trying to refresh the full page you should try this. if you want to display some other page instead upon 6 failed attempts you could either try using window.location.href = "some other URL here"
Hope this solves your query.

I was able to fix it. Here is how I was able to make it run properly:

When the user ran out of guesses or guess the correct work the id of the submit button gets changed to startNew. Then I added a double condition to the empty text check where the code will check if the text box is empty but also the code will check if the id for the submit button is guestGuess.

That is where I was having issues because when I was clicking an empty window that part of the code was running as well.

I updated the code here http://liveweave.com/SZDKHz

Thank you so much for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.