Skip to content Skip to sidebar Skip to footer

If Statement Doesnt Work When Included With Two Variables

I want to create a simple login system with the help of javascript. Unfortunately, the if statement doesn't work as I've intended it to. The page doesn't redirect when the enter ke

Solution 1:

myInput is undefined for your event, you should get the value by adding following line in your eventListener.

 let myInput= document.getElementById("myInput").value;

Change your code like following.

var input = document.getElementById("myInput");
input.addEventListener("keyup", function(event) {
  let myInput = document.getElementById("myInput").value;
  for (i = 0; i < objPeople.length; i++) {
    if (myInput == objPeople[i].studentid && event.keyCode === 13) {
      console.log(objPeople[i].studentid);
      window.location.href = "https://www.google.com";
    }
  }
});

Edit: Sample

var objPeople = [{
    studentid: "input1"
  },
  {
    studentid: "input2"
  }
]

var input = document.getElementById("myInput");
input.addEventListener("keyup", function(event) {
  let myInput = document.getElementById("myInput").value;
  for (i = 0; i < objPeople.length; i++) {
    if (myInput == objPeople[i].studentid && event.keyCode === 13) {
      console.log(objPeople[i].studentid);
      window.location.href = "https://www.google.com";
    }
  }
});
<html>

<head>
  <script LANGUAGE="JavaScript" src="JavaScript.js"></script>
  <title>aaa</title>
</head>

<body>
  <input type="text" id="myInput">
</body>

</html>

Post a Comment for "If Statement Doesnt Work When Included With Two Variables"