How to create a search bar that DOESN'T GIVE RESULTS WHEN NOTHING IS WRITTEN ON IT. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to create a search bar that DOESN'T GIVE RESULTS WHEN NOTHING IS WRITTEN ON IT.

My JavaScript code so far: function search() { // Declare variables var input, filter, ul, li, a, i; input = document.getElementById('input'); filter = input.value.toUpperCase(); ul = document.getElementById('ops'); li = ul.getElementsByTagName('li'); // Loop through all list items, and hide those who don't match the search query for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName("a")[0]; if (a.innerHTML.toUpperCase().indexOf(filter) > 1) { li[i].style.display = "block"; } else{ li[i].style.display = "none"; } if (a.innerHTML.toUpperCase().indexOf(filter) <= -1) { li[i].style.display = "none"; } } }

9th Aug 2018, 9:31 AM
AquA217
AquA217 - avatar
2 Answers
+ 6
<input type=search> <button>Search</button> don't gives you any result
9th Aug 2018, 9:59 AM
Sudarshan Rai
Sudarshan Rai - avatar
0
Confused about the question format, but //eventListener for keydown if (event.key = "Enter" || event.keyCode == 13) { if (input.value != "") { search(); } }
9th Aug 2018, 8:29 PM
ghostwalker13
ghostwalker13 - avatar