Advertisement
viligen

searchInList_DOM

May 29th, 2022
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function search() {
  2.     let matches = Array.from(document.querySelectorAll("#towns li"));
  3.     matches.map(
  4.         (m) => (m.style = "font-weight: normal; text-decoration: none")
  5.     );
  6.     let resultNum = 0;
  7.     let searchedText = document.getElementById("searchText").value;
  8.  
  9.     let resultEl = document.getElementById("result");
  10.     resultEl.textContent = 0;
  11.  
  12.     for (let i = 0; i < matches.length; i++) {
  13.         if (matches[i].textContent.includes(searchedText)) {
  14.             matches[i].style = "font-weight: bold; text-decoration: underline";
  15.          
  16.             resultNum += 1;
  17.         }
  18.     }
  19.     resultEl.textContent = `${resultNum} matches found`;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement