Skip to content

Instantly share code, notes, and snippets.

@mojzis
Created July 13, 2023 05:56
Show Gist options
  • Save mojzis/d2a4566d4b63163eacd1570ffc680d54 to your computer and use it in GitHub Desktop.
Save mojzis/d2a4566d4b63163eacd1570ffc680d54 to your computer and use it in GitHub Desktop.
js search in nav
let searchBox = document.querySelector(".searchBox");
let filterSearch, nav, linkCollection;
function processResults() {
{#input = document.getElementById("mySearch");#}
filterSearch = searchBox.value.toUpperCase();
nav = document.querySelector(".leftnav");
linkCollection = nav.getElementsByClassName("menuitem");
Array.from(linkCollection).forEach(element => {
if (element.innerHTML.toUpperCase().indexOf(filterSearch) > -1) {
element.style.display = "";
} else {
element.style.display = "none";
}
});
}
searchBox.addEventListener("keyup", () => {
processResults();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment