Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

password form

Hi, I'm trying to password protect my portfolio website. and I've written some code of the password form.

However, I have to click on the 'submit' button in order to enter password. When I change the input type in "submit", this actually solves the problem above, however, this way creates another problem. It won't link to the index2.html that I directed it to. When the input type is "button" this link works perfectly.

---------(CODE BELOW)-------------------------------------------------------

<div class="overlay_top"> <div class="row justify-content-center" style="padding-top: 10%;"> <form name="login"> <input type="password" id="pswd" class="form-control mx-sm-3" aria-describedby="passwordHelpInline" placeholder="Password" style="border-radius: 0rem; width:240px;"> <input type="submit" id="submitbutton" value="Submit" onclick=checkPswd() class="btn btn-white" style="border-radius: 0rem; width:240px; font-weight: 700;"> </form> </div> </div>

    <script>
      function checkPswd() {
          var confirmPassword = "admin";
          var password = document.getElementById("pswd").value;
          if (password == confirmPassword) {
              window.location.href="index2.html";
          }
          else{
              alert("Passwords do not match.");
          }
      }
    </script>