+ 2
What is problem in the given code(it is a code about filter search list)
Plz correct the given code it's about filter search list. https://code.sololearn.com/WEzm90hSVho6/?ref=app
1 Odpowiedź
+ 3
There are a few problems in your search function.
- Your li should be calculated using querySelectorAll instead of querySelector.
- Your indexof in the loop should be replaced with indexOf. indexof doesn't exist.
- very minor but you should declare your "filter" variable using "var" so it isn't global.
Here is the corrected version that works:
function search(){
var input = document.getElementById("input");
var ul = document.getElementById("data");
var li = document.querySelectorAll("li");
var filter = input.value.toUpperCase();
for(var i =0;i < li.length;i++){
var a = li[i].getElementsByTagName("a")[0];
var text = a.textContent || a.innerHTML;
if(text.toUpperCase().indexOf(filter) > -1){
li[i].style.display ="";
}
else{
li[i].style.display = "none";
}
}
}