why does it reset back? Is there a way to make it not reset so I can make sure it works?
would the preventDefault() work
How do I use it if I add the event like
Numbers.onsubmit = checkInput
why does it reset back? Is there a way to make it not reset so I can make sure it works?
would the preventDefault() work
How do I use it if I add the event like
Numbers.onsubmit = checkInput
With that old way of attaching the event you need to return false.
For preventDefault to work you need to attach the event properly using an event listener.
ok made this change
Numbers.onsubmit = checkInput;
to
document.getElementById("Numbers").addEventListener("onsubmit", function(event){
event.preventDefault();
checkInput();
});
but now nothing happens if I press the submit button after entering a # in, how do I get the checkInput function to run then?
The event is called âsubmitâ - none of the event names have âonâ on the front - that is just how they were referenced with the old event handlers.
ok, made the edits
<form id="Numbers" onSubmit="checkInput()">
<input type="text" id="myNumber" placeholder="Please Enter a Whole Number (1-999)">
<input type="submit" value="Start Guessing!">
</form>
Which seems to work, but had to add the event listener to the script
document.getElementById("Numbers").addEventListener("submit", function(event){
event.preventDefault();
});
which seems to prevent the page from refreshing,
http://fixmysite.us/Web_Programming_With_Javascriipt/Assignment5/
but my logic is screwed up, it always says I lost when I should win though
Its like this doesnât work
if (guess == input) {
document.getElementById('output').innerHTML = '<p><img src="images/winner.png"></p><h2 id="winner">You Won!!!</h2>';
turns = 0;
but then why does this seem to work
if (guess < input)
if I try to see both variablesâŚ
console.log(input);
consoee.log(guess);
and heres the result
if (turns == 0)
//the loser message
that was the issue, it seemed to resolve itself if I replaced turns = 0 to a break to get out of the loop
I found a strange bug, I enter 8 as the number, then I make random guessesâŚthis happens
do {
//prompt the other user for a guess
var guess = prompt(hint +' You have ' + turns + ' guesses left.');
//ensure they entered something
console.log(guess);
console.log(input);
if (!guess)
break;
if (guess == input) {
document.getElementById('result').innerHTML = '<p><img src="images/winner.png"></p><h2 id="winner">You Won!!!</h2>';
break;
} else {
hint = 'Nope.';
if (guess < input)
document.getElementById('guesses').innerHTML += '<li>Your Number: <b>'+guess+'</b> is too small!</li>';
if (guess > input)
document.getElementById('guesses').innerHTML += "<li>Your Number: <b>"+guess+"</b> is too big!</li>";
turns--;
}
} while (turns > 0)
Do you see what the problem is?
it look like the problem was fixed when I converted both variables to a number type like
input = Number(input);
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.