Advertisement
Kamend1

04.Search in list

Mar 20th, 2025 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.    const listItems = document.querySelectorAll('#towns li');
  3.    const searchText = document.getElementById('searchText').value.toLowerCase();
  4.    let results = [];
  5.    
  6.    for (let item of listItems) {
  7.       item.style.fontWeight = 'normal';
  8.       item.style.textDecoration = 'none';
  9.    }
  10.  
  11.    for (let item of listItems) {
  12.       if (item.textContent.toLowerCase().includes(searchText)) {
  13.          item.style.fontWeight = 'bold';
  14.          item.style.textDecoration = 'underline';
  15.          results.push(item);
  16.       }
  17.    }
  18.  
  19.    let resultString = `${results.length} matches found`;
  20.  
  21.    document.getElementById('result').innerHTML = resultString;
  22.  
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement