I need to search content based on the input provided. For eg) if i provide k as input it should filter the content and show names akila and place uk. Problem faced here is first element is being filtered.Html elements are stacked as shown in the below code
function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
//console.log(li[i].children)
a = li[i].getElementsByTagName("span")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
<ul id="myUL">
<li>
<a href="#">Names</a>
<!--ul id 1-->
<ul id="corp">
<li><a href="#"><div><span>akila</span></div></a></li>
<li><a href="#"><div><span>agnes</span></div></a></li>
</ul>
</li>
<li>
<a href="#">Place</a>
<!--ul id 2-->
<ul id="region">
<li><a href="#"><div><span>USA</span></div></a></li>
<li><a href="#"><div><span>UK</span></div></a></li>
</ul>
</li>
</ul>