Advertisement
TZinovieva

Search in List JS Advanced

Jul 27th, 2023
1,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function search() {
  2.    let towns = document.querySelectorAll('#towns li');
  3.    let currentSearch = document.getElementById('searchText').value;
  4.    let result = document.getElementById('result');
  5.  
  6.    let matchCounter = 0;
  7.    let arr = Array.from(towns);
  8.    
  9.    for (let town of arr) {
  10.  
  11.       if(town.textContent.includes(currentSearch)) {
  12.          matchCounter++;
  13.          town.style.fontWeight = 'bold';
  14.          town.style.textDecoration = 'underline'
  15.       }
  16.    }
  17.    result.textContent = `${matchCounter} matches found`;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement