Solution 1 :

You change type of input to “submit”

<input class="gradient-button" type="submit" value="JOIN NOW" onclick="checkPassword()" />

Moreover, remove onClick and add onSubmit event with checkPassword function on form tag

Problem :

The form below works, but only when the button is clicked. Can someone please tell me how to submit this form and check the password with the script below when the enter key is pressed as well?

function checkPassword() {
  if (document.getElementById('password').value == 'abc123') {
    location.href = "./yes";
  } else {
    location.href = "./no";
  }
}
<div class="login-box">
  <form>

    <div class="user-box">
      <input type="password" id="password">
      <label>PASSWORD</label>
    </div>

    <div class="gradient-button-container">
      <input class="gradient-button" type="button" value="JOIN NOW" onclick="checkPassword()" />
    </div>

  </form>
</div>

Comments

Comment posted by cedexious

Thanks so much. As it turns out, a return false was also necessary. In case anyone comes across this thread and wants the working code, you just need to change the form tag above to

and the submit tag to

By