Isaac OConnor Isaac OConnor - 2 months ago 10
HTML Question

JavaScript not working on website

I'm pretty new to coding, so this block of code is really bad.

I'm making a simple log in system really quick but now I've been sucked in and can't figure out whats wrong with my code. Help is greatly appreciated.

The username and password is "admin" (I know, it's bad, but this website won't be public).

HTML:



function loginUser() {
var usernameId;
usernameId = document.getElementById("username1").value;
if (usernameId == "admin") {
window.location = "home.html";
}
}
function loginPassword() {
var passwordId;
passwordId = document.getElementById("password1").value;
if (passwordId == "admin") {
loginUser();
}
}

<form class="login-form">
<input type="text" name="username1" placeholder="username"/>
<input type="password" name="password1" placeholder="password"/>
<button onClick="loginPassword()">login</button>




Answer

You're using name="username1" and name="password1", but you're then getting the elements with getElementById(). If so, you need to use the id attribute instead of the name attribute:

function loginUser() {
  var usernameId;
  usernameId = document.getElementById("username1").value;
  if (usernameId == "admin") {
    window.location = "home.html";
  }
}
function loginPassword() {
  var passwordId;
  passwordId = document.getElementById("password1").value;
  if (passwordId == "admin") {
    loginUser();
  }
}
<form class="login-form">
  <!--               v---- changed here       ---->
  <input type="text" id="username1" placeholder="username"/>
  <!--                   v---- and here       ---->
  <input type="password" id="password1" placeholder="password"/>
  <button onClick="loginPassword()">login</button>
</form>

Comments