Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Can't trigger button with ENTER key

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #271997
    J.J
    Participant

    Hello there.

    I’m new with javascript and was trying to make a simple password system that if correct would lead the user from the current page to a new one. I found this example online (can’t remember where, sorry) that works well but can’t add the “triggered by ENTER key” function to it. I was hoping someone could tell me what am I doing wrong. It’s probably something stupidly simple but I just can’t figure it out.

    This is the code so far:

    https://codepen.io/jesustakethehtml/pen/jKbdbW

    #271998
    Shikkediel
    Participant

    Give this a try…

        function checkPswd() {
          var confirmPassword = "pug.life";
          var password = document.getElementById("pswd").value;
          if (password == confirmPassword) {
            window.location = "welcome.html";
          } else {
            alert("Passwords do not match.");
          }
        }
    
        document.getElementById("pswd").addEventListener("keyup", function(event) {
          event.preventDefault();
          if (event.keyCode === 13) {
            document.getElementById("myBtn").click();
          }
        });
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.