title: Why isn't my "window.location =" line working?
body: <p>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.</p>
  
  <p>Here's my code:</p>
  
  <pre><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
  </code></pre>
  
  <p>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...</p>
  
  <p>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.</p>
  
  <p><strong>UPDATE:</strong><br>
  I've noticed that when I change...  </p>
  
  <pre><code>if (pwinput === correct) {
  </code></pre>
  
  <p>...to...</p>
  
  <pre><code>if (pwinput === "") {
  </code></pre>
  
  <p>...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!</p>
  
  <p><strong>UPDATE TWO:</strong><br>
  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.</p>
  
  <p>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.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;head&gt;
      &lt;title&gt;First Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="pwform" name="myform"&gt;
          &lt;input id="pw" type="password"&gt;
      &lt;/form&gt;
      &lt;button id="btn"&gt;run test function&lt;/button&gt;
      &lt;a href="newpage.html"&gt;link to next page&lt;/a&gt;
  &lt;script type="text/javascript"&gt;
      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;    // &lt;-- I don't know what the
      document.getElementById("btn").onclick = testfunction;        // &lt;-- difference between these is.
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>Is there syntax or something I'm not following?</p>
  
  <p><strong>ADDENDUM:</strong><br>
  I know this isn't the "right" way to do a password. I'm just getting practice on some of the basics.</p>