nomarie
-
Posts
4 -
Joined
-
Last visited
Content Type
Profiles
Forums
Events
Posts posted by nomarie
-
-
Doesn't seem to be working for me - do any changes need to be made to the original for loop?
-
I had thought about using a class rather than ID but am not sure how to approach that.
-
I'm basically trying to replicate https://www.w3schools.com/howto/howto_js_filter_lists.asp but with multiple ULs rather than a single one. I've had no problem with the HTML/CSS adaptations, but am stuck on the JS. Can anyone help?
Filter multiple lists
in JavaScript
Posted
I've got two ULs with IDs 'ul1' and 'ul2'.
JS:
function journalsearch() {
// Declare variables
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
ul = document.getElementById('ul1','ul2');
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.getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li.style.display = "";
} else {
li.style.display = "none";
}
}
}
Search box:
<input type="text" id="input1" onkeyup="journalsearch('ul1','ul2')" placeholder="Search for titles...">
Search filters the first list as expected, but nothing happens to the second.