0

I want to make the link in this change depending on whether the password is correct. I want to set one password and I only know html and minimal JS. I think I have it set so that when the password is wima it will change the href and allow the link to work. That doesn’t happen. Can I have some help?

function login()
var password = getElementById("password"); {
  if (password = "wima") {
    getElementById("submit").href = "/pages/home.html";
  } else {
    getElementById("submit").href = "index.html";
  }
}
<p>

  Username
  <input id="username" type=text placeholder="WIMA"><br> Password
  <input id="password" type=password placeholder="WIMA"><br>

  <a class="button" id="submit" href="#" onclick="login()">
    Submit 
    </a>
</p>

5
  • var password = getElementById("password"); should be var password = document.getElementById("password").value; Commented Dec 12, 2018 at 15:03
  • setAttribute('href', "link.html"); Commented Dec 12, 2018 at 15:05
  • Thank you. Is it possible to set the href of my link when it checks then immediately take me to the link? When I click the submit link I want the code to run and to be taken to the correct page. Commented Dec 12, 2018 at 15:09
  • Where do I put that setAttribute? Commented Dec 12, 2018 at 15:12
  • @Nathan Forget about that, you can set .href directly, like you did. As for getting taken to the correct page, this will indeed happen automatically after your onclick function has finished, since it doesn't prevent the standard "user clicked on <a>" behavior. Commented Dec 12, 2018 at 15:51

5 Answers 5

1

There are a few issues with your JavaScript.

<script language="JavaScript">

function login()
var password = getElementById("password"); // this gets the element, not the value of the element
{ // this curly brace is in the wrong place
if (password = "wima") { // this sets the value of the password var to "wima" 
getElementById("submit").href="/pages/home.html";
}
else {
getElementById("submit").href="index.html";
}
} 
</script>

Here is your code, cleaned up.

<script language="JavaScript">

function login() {
    var password = document.getElementById("password").value;
    if (password == "wima") { // use == to compare value
        document.getElementById("submit").href="/pages/home.html"; 
    }
    else {
        document.getElementById("submit").href="index.html";
    }
}
</script>

Another issue here is that you shouldn't be changing the href on the element used to execute the login() function.

You could redirect the user to the new page like so:

   <script language="JavaScript">

    function login() {
        var password = document.getElementById("password").value;
        if (password == "wima") { 
            window.location.href="/pages/home.html";
        }
        else {
            window.location.href="index.html";
        }
    }
    </script>
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you are doing it wrong if you want to change the href value based upon input type text. You should make a blur/change event on password input text. Based upon password value when user clicks on href he should be redirected accordingly. Check this out:

function login() {
        var _password = document.getElementById("password").value;
        if ("wima" == _password) {
            document.getElementById("submit").href = "/pages/home.html";
        } else {
            document.getElementById("submit").href = "index.html";
        }
    }
<p>

        Username
        <input id="username" type=text placeholder="WIMA">
        <br> Password
        <input id="password" type=password placeholder="WIMA" onblur="login()">
        <br>

        <a class="button" id="submit" href="#">
    Submit 
    </a>
    </p>

Comments

0

Here is a form validator with a switch.

function validateForm() {
  var x = document.forms["myForm"]["password"].value;

  switch (x) {
    case "":
      alert("Name must be filled out");
      return false;
      break;

    case "wima":
      return true;
      break;

    default:
      alert("Error: Wrong Password.");
      document.location.href = "https://stackoverflow.com/search?q=notloggedin";
      // Replace the link above with your error link return
      return false;
  }

}
<!-- Replace action link with your successful link -->
<form name="myForm" action="https://stackoverflow.com/search?q=login" onsubmit="return validateForm()" method="post">
  <input type="password" name="password">
  <input type="submit" value="Submit">
</form>

Comments

0

Your password is visible in text if someone inspects the html/javascript. So this method of security is not advised. For basic concepts it is interesting to have a link change based on input. Try this.

<p>

  Username
  <input id="username" type="text" placeholder="WIMA"><br> Password
  <input id="password" type="password" placeholder="WIMA"><br>

  <a class="button" id="submit" >
    Submit 
    </a>
</p>
<script>
    var password = document.getElementById('password');
    password.addEventListener('change', enableLogin);
    var submit = document.getElementById('submit');

    function enableLogin() {
      if (password.value == "wima") { // it is an easy mistake (= or ==)
        submit.href = "/pages/home.html";
      } else {
        submit.removeAttribute('href');
      }
    }
</script>

Comments

0

A few things happened here:

  • The value inside a <input> is accessed by .value;
  • You misplaced the {
  • getElementById is not a global method it has to be called on the element you want to select in (in your case the document itself)
  • To test if two values are equal use === in js

function login() {
  var password = document.getElementById("password").value;
  if (password === "wima") {
    document.getElementById("submit").href = "/pages/home.html";
  } else {
    document.getElementById("submit").href = "index.html";
  }
}
<p>

  Username
  <input id="username" type="text" placeholder="WIMA"><br> Password
  <input id="password" type="password" placeholder="WIMA"><br>

  <a class="button" id="submit" href="#" onclick="login()">
    Submit 
    </a>
</p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.