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">

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

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



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