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="https://stackoverflow.com/questions/54849290/JavaScript.js"></script>
<title>aaa</title>
</head>
<body>
<input type="text" id="myInput">
</body>
</html>
2
solved if statement doesnt work when included with two variables