Advertisement
braveheart1989

2.Search in List

Oct 18th, 2016
197
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.     <title>Title</title>
  6.     <script src="jquery-3.1.1.min.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.         return function () {
  21.             let ul = $('#towns li').toArray();
  22.             let searchText = $('#searchText').val();
  23.             let counter = 0;
  24.             for (let li of ul) {
  25.             if ($("li:contains(searchText)")) {
  26.                 $(li).css('font-weight', 'bold');
  27.             }
  28.             else{
  29.                 $(li).css('font-weight', '');
  30.             }
  31.             $('#result').text(counter + " matches found.");
  32.  
  33.         }
  34.     }
  35.  
  36. </script>
  37. </body>
  38. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement