Live Search Filter

Concepts Used: keyup(), filtering elements

Features:

  • Search through a list dynamically
<!DOCTYPE html>
<html>
<head>
  <title>Live Search</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
  <h2>Live Search Filter</h2>
  <input type="text" id="search" placeholder="Search...">
  <ul id="list">
&lt;li&gt;Apple&lt;/li&gt;
&lt;li&gt;Banana&lt;/li&gt;
&lt;li&gt;Orange&lt;/li&gt;
&lt;li&gt;Mango&lt;/li&gt;
&lt;li&gt;Grapes&lt;/li&gt;
</ul> <script>
$(document).ready(function(){
  $("#search").keyup(function(){
    let value = $(this).val().toLowerCase();
    $("#list li").filter(function(){
      $(this).toggle($(this).text().toLowerCase().indexOf(value) &gt; -1);
    });
  });
});
</script> </body> </html>

Explanation:

  • .keyup() detects input changes.
  • .filter() checks list items dynamically.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *