Unable to figure out why 'onChange' event is not firing with change in the value of . The onChange event fires when I hit enter when the is in focus. I have added the 'change' event to the tag in a seperate JS file.
The basic function of the JS file is to take value from the and compare it with some strings, and output the matching strings.
//name of fruits
let data=["apple","watermelon","orange","strawberry","grape","cherry","mango","nectarine","banana","pomegranate","raspberry","papaya","kiwi","pineapple","lemon","apricot","grapefruit","peach","avocado","passion fruit","plum","lime","blueberry","lychee","fig"]
//------------------------------------//
//Grabbing important elements
let inputField = document.getElementsByTagName("input")[0];
let ul = document.getElementsByTagName("ul")[0];
//bindings
inputField.addEventListener("change",onChangeHandler);
//functions
function onChangeHandler(){
//empty the ul before inserting anything
ul.innerHTML="";
let queryString = inputField.value;
if(queryString==""){
return;
}
for(i=0;i<data.length;i++){
if(data[i].indexOf(queryString)==0){
let li = document.createElement("li");
li.innerText = data[i];
ul.appendChild(li);
}
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div id="container">
<div id="search">
<input type="text">
</div>
<ul id="output">
</ul>
</div>
<script src="script.js"></script>
</body>
</html>