I'm very new to coding. I apologize if this is a simple or insultingly basic question (this is my first time posting here). I have no idea why my javascript isn't working, and I don't know what resources to use to find out on my own, as it seems (to me) to be a sort of oddly specific situation.
Here's my code:
var correct = "password"; //Set "correct" to the correct password
function isenter(){
var pressed = event.which;
if (pressed === 13){ //If key pressed is "Enter"
var pwinput = document.getElementById("pw").value; //Set "pwinput" to the value of the password input field (id="pw")
window.alert(correct + " " + pwinput); //Display values of correct and entered password (for debugging)
if (pwinput === correct) { //If entered password is equal to correct password
window.alert("Match registered"); //Display "Match Registered" (for debugging)
window.location = "lobby.html"; //Redirect to another page (THIS IS WHAT ISN'T WORKING)
}
else{
window.alert("Match NOT registered") //Display "Match NOT registered" (for debugging)
}
}
}
window.addEventListener("keypress", isenter) //Listen for keypresses. Run isenter when keys are pressed
The page will not redirect(line 10). I can't think of why it won't. The line directly above it confirms that a match has been registered, and that that particular branch of code is in fact running, but line 10 does not work properly for some reason. If I ctrl-x line 10 and paste it between line 4 and 5, it works fine (redirecting to the new page whenever a key is pressed), telling me that it SHOULD work, but doesn't due to its location for some reason. The only thing I can think is that being inside the "if" statement is preventing it from working properly, but I don't know why this should be the case...
Any help would be much appreciated! Again, this is my first time posting here. sorry if I'm breaking any protocol or conventions or if my code is a wreck. Thank you in advance! I know I'm probably missing something stupid basic.
UPDATE:
I've noticed that when I change...
if (pwinput === correct) {
...to...
if (pwinput === "") {
...then the redirect is successful whenever I hit "Enter". It seems like the field NOT being empty is preventing the redirect somehow. I'm still not sure what's going on. I hope this added information helps!
UPDATE TWO:
After removing a pair of "form" tags from around the "pw" input, the code now works as expected. I'm currently trying to figure out how to get things working without removing the form tags.
On the suggestion of @tim, I've switched to using "onsubmit" with the form. This simplifies my code a bit, but I'm still running into the same problem. The code around the redirect is running, but the redirect itself isn't working. Here's a barebones html sample with the javascript embedded. I have a form, a button, and a link. The link and the button work fine, but the form still won't redirect.
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<title>First Page</title>
</head>
<body>
<form id="pwform" name="myform">
<input id="pw" type="password">
</form>
<button id="btn">run test function</button>
<a href="newpage.html">link to next page</a>
<script type="text/javascript">
function testfunction(){
window.alert("Test function ran."); //Verify that testfunction is running.
window.location ="newpage.html"; //Redirect (works on button, not on form)
}
document.getElementById("pwform").onsubmit = testfunction; // <-- I don't know what the
document.getElementById("btn").onclick = testfunction; // <-- difference between these is.
</script>
</body>
</html>
I'm confused, because I don't think I have any reason to expect a difference in the code being run from the button and that being run from the submission of the form. The alert pops up when submitting the form, confirming that the code is running, but the redirect itself isn't working.
Is there syntax or something I'm not following?
ADDENDUM:
I know this isn't the "right" way to do a password. I'm just getting practice on some of the basics.