Perform a case-insensitive search for items in a table: - Javascript jQuery

Javascript examples for jQuery:Table

Description

Perform a case-insensitive search for items in a table:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").each(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });/*from   w w  w.  j a  v a 2 s.c om*/
  });
});
</script>
<style>
table {
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head>
<body>

<h2>Filterable Table</h2>
<p>Type something in the input field 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>T</td>
      <td>D</td>
      <td>a@example.com</td>
    </tr>
    <tr>
      <td>M</td>
      <td>M</td>
      <td>b@example.com</td>
    </tr>
    <tr>
      <td>J</td>
      <td>D</td>
      <td>c@example.com</td>
    </tr>
    <tr>
      <td>A</td>
      <td>A</td>
      <td>d@example.com</td>
    </tr>
  </tbody>
</table>

</body>
</html>

Related Tutorials