How to Create Filters using jQuery

How to Create Filters using jQuery

In this article, you will learn How to Create Filters using jQuery.

Example:-

Method of Filter Tables

<html>
<head>
<title>Filter Table<title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>
<style>
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}
td,th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}
</style>
</head>
<body>
<h2>Filter Table</h2>
<p>Type something in input box to search the table for first names, last names or emails:</p>  
<input id="myInput" type="text" placeholder="Search..">
<br><br>
<table>
  <thead>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Email</th>
  </tr>
  </thead>
  <tbody id="myTable">
  <tr>
    <td>Amandeep</td>
    <td>singh</td>
    <td>amansingh@example.com</td>
  </tr>
  <tr>
    <td>Ram</td>
    <td>Kumar</td>
    <td>ramkumar@email.com</td>
  </tr>
  <tr>
    <td>Priyanka</td>
    <td>Mehta</td>
    <td>priyanka.mehta@mail.com</td>
  </tr>
  </tbody>
</table>
</body>
</html>

Filter the List

<!DOCTYPE html>
<html>
<head>
<title>Filter List using Jquery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myList li").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>
</head>
<body>
<h2>Filter List</h2>
<p>Type something in the input field to search the list</p>  
<input id="myInput" type="text" placeholder="Search..">
<br>
<ul id="myList">
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
  <li>Fourth</li>
</ul> 
</body>
</html>

Filter Anything

<html>
<head>
<title>Filter Anything using jQuery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myDIV *").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>
</head>
<body>
<h2>Filter Anything</h2>
<p>Type something in the input box to search for a specific text inside the div element</p>
<input id="myInput" type="text" placeholder="Search..">
<div id="myDIV">
  <p>This is a First paragraph.</p>
  <div>This is a div element inside the div.</div>
  <button>This is the a button</button>
</div>
</body>
</html>

Read on Facebook

Leave A Reply