How to Live search on the HTML table with jQuery

Whenever there is a large list of records that you are displaying in the HTML table. In this case, it’s better to allow the users to search text within the table.

You can easily do this on your Client-side with jQuery.

For searching text within HTML table columns I am using .contains() method.

How to Live search on the HTML table with jQuery


Contents

  1. HTML
  2. jQuery
  3. Demo
  4. Conclusion

1. HTML

I have created two textboxes for searching value and a <table> with some records.

In the <table> added a <tr class='notfound'> at the last which display when no record found while search.

Completed Code

<style>
.notfound{
  display: none;
}
</style>
<input type='text' id='txt_searchall' placeholder='Enter search text'>&nbsp; 
<input type='text' id='txt_name' placeholder='Enter search name'>
<br/>
<table width='100%' border='1' style='border-collapse:collapse;' >
 <thead>
  <tr>
   <th>S.no</th>
   <th>Name</th>
   <th>Course</th>
   <th>City</th>
  </tr>
 </thead>
 <tbody>
   <tr>
     <td>1</td>
     <td>Yogesh singh</td>
     <td>M.SC</td>
     <td>Bhopal</td>
   </tr>
   <tr>
     <td>2</td>
     <td>Sonarika Bhadoria</td>
     <td>BE</td>
     <td>Pune</td>
   </tr>
   <tr>
     <td>3</td>
     <td>Vishal Sahu</td>
     <td>BE</td>
     <td>Indore</td>
   </tr>
   <tr>
     <td>4</td>
     <td>Sunil Patel</td>
     <td>MBA</td>
     <td>Bhopal</td>
   </tr>
   <tr>
     <td>5</td>
     <td>Anil Singh</td>
     <td>MCA</td>
     <td>Delhi</td>
   </tr>
   <!-- Display this <tr> when no record found while search -->
   <tr class='notfound'>
     <td colspan='4'>No record found</td>
   </tr>
 </tbody>
</table>

2. jQuery

Define keyup event on both textboxes.

Search all columns

The first textbox use to search value on all columns.

When keyup event triggers then hide all <tbody><tr> and search value on all  <td> of <tbody><tr>. If value found then show the <tr>.

Display <tr class='notfound' > if not record found.

Particular Column

The second textbox use to search value on name column.

Use nth-child selector to select Name column by passing index position (2) in it.

The :contains() search is case sensitive for making it case-insensitive I have added script after document ready. You can remove it if you don’t want.

Completed Code

$(document).ready(function(){

  // Search all columns
  $('#txt_searchall').keyup(function(){
    // Search Text
    var search = $(this).val();

    // Hide all table tbody rows
    $('table tbody tr').hide();

    // Count total search result
    var len = $('table tbody tr:not(.notfound) td:contains("'+search+'")').length;

    if(len > 0){
      // Searching text in columns and show match row
      $('table tbody tr:not(.notfound) td:contains("'+search+'")').each(function(){
        $(this).closest('tr').show();
      });
    }else{
      $('.notfound').show();
    }

  });

  // Search on name column only
  $('#txt_name').keyup(function(){
    // Search Text
    var search = $(this).val();

    // Hide all table tbody rows
    $('table tbody tr').hide();

    // Count total search result
    var len = $('table tbody tr:not(.notfound) td:nth-child(2):contains("'+search+'")').length;

    if(len > 0){
      // Searching text in columns and show match row
      $('table tbody tr:not(.notfound) td:contains("'+search+'")').each(function(){
         $(this).closest('tr').show();
      });
    }else{
      $('.notfound').show();
    }

  });

});

// Case-insensitive searching (Note - remove the below script for Case sensitive search )
$.expr[":"].contains = $.expr.createPseudo(function(arg) {
   return function( elem ) {
     return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
   };
});

3. Demo

View Demo


4. Conclusion

Above script filter your table content on the Client side and its only works with text type elements like – <label> ,<span>, <p>, etc., or if there is just text.

If you want this to work with input elements like – textbox, textarea also then you need to customize the script.

If you found this tutorial helpful then don't forget to share.

16 thoughts on “How to Live search on the HTML table with jQuery”

  1. thank you very much for this code. i tried it and it worked but what if i want to show a “no record found” instead of the blank tbody it shows? i tried 2 codes but they aint working.

    Thank you

    Reply
  2. Thanks Yogesh.
    Your code is exactly what I wanted. But one thing I need is you hide these rows initially and should be visible when searched, I mean table must be hidden until we put any value in search and it must show the matching data row. Can you help me please?

    Reply
  3. Hey, Thanks for the awesome post.
    You solved my headache in a single post!
    Follow up question:
    I expanded on your script to have 5 different search box that searches within 5 different columns.
    Now I’m trying to see if I can use the search boxes simultaneously, so that I can use them as a filter mechanism.
    For example, in your example, you listed two person with last name “Singh.”
    Now within that search result, lets say we want to further filter the result so that only the person from Delhi is shown (by adding a search box for the cities).
    What would be the most elegant solution to this?

    Thanks!

    Reply
  4. This is great post.
    Helped a lot to learn. I am new to scripting.
    But when I used your script, it seems like broken for my table. On search name the entire ‘tr’ is not shown and on text search one column goes missing.
    Here is the code : https://jsfiddle.net/x542wk3m/
    Can you guide/fix where is the issue ? on search i want the entire to be displayed.

    Reply
  5. Hi Yogesh and thanks for the script.
    I have a problem. When I search in the Name column for “el”, I also get the result from the column City.

    Searching for “el” gives the result:
    • Row number 4 display Sunil Patel (Name column) and
    • Row number 5 display Delhi in (City column).

    Is there anything you can fix so that only the result from the Name column is displayed?

    Reply

Leave a Comment