Advertisement
valchak

Search in List

Feb 20th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="   crossorigin="anonymous"></script>
  6.     <!--<script src="search.js"></script>-->
  7. </head>
  8. <body>
  9. <ul id="towns">
  10.     <li>Sofia</li>
  11.     <li>Pleven</li>
  12.     <li>Varna</li>
  13.     <li>Plovdiv</li>
  14. </ul>
  15. <input type="text" id="searchText" />
  16. <button onclick="search()">Search</button>
  17. <div id="result"></div>
  18. <script>
  19.     function search() {
  20.         let targetValue = $('#searchText').val();
  21.         let counter = 0;
  22.         $('#towns').find('li').each((index, el) =>
  23.         {
  24.             if (el.textContent.includes(targetValue)) {
  25.                 $(el).css('font-weight', 'bold');
  26.                 counter++;
  27.             }
  28.         });
  29.  
  30.         $('#result').text(counter + ' matches found.')
  31.     }
  32. </script>
  33. </body>
  34. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement