What is problem in the given code(it is a code about filter search list) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

7th Nov 2020, 4:01 PM
Abhishek Kumar
Abhishek Kumar - avatar
1 Answer
+ 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"; } } }
7th Nov 2020, 7:04 PM
Josh Greig
Josh Greig - avatar