How to perform a real time search and filter on a HTML table? - GeeksforGeeks

31 Oct, 2019
Here, we learn how to perform a real-time search and filter on an HTML table. When a word is entered, all rows of the table will be searched (except the table head) and the rows containing the matching word will be displayed. For this, we can use JQuery methods.
filter(): This method is used to filter out all the elements that do not match the selected criteria and those matches will be returned. Reduce the set of matched elements to those that match the selector or pass the function’s test.
toggle(): This method is used to check the visibility of selected elements to toggle between hide() and show() for the selected elements. Display or hide the matched elements.
In the below example, the search value entered in the search box is stored in the variable named “value” and converted to lowercase since we are doing case insensitive search. After that, we search each row in the table using the filter() function and display the row where the string stored in variable “value” is found. The toggle() method is used to show the rows containing the search word and hide others. The indexOf(value) returns -1 if the word is not found in the row.
Example: Below example illustrates the use of filter() and toggle() function to perform a realtime search and filter on HTML table.
<!DOCTYPE html><html>  <head>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">    </script>      <style>        table {            font-family: arial, sans-serif;            border-collapse: collapse;            width: 50%;        }                  td,        th {            border: 1px solid #dddddd;            text-align: left;            padding: 8px;        }                  h1 {            color: green;        }    </style></head>  <body>    <center>        <h1>GeeksforGeeks</h1>        <h3>          perform a real time search and filter           on a HTML table        </h3>        <b>Search the table for Course, Fees or Type:           <input id="gfg" type="text"                  placeholder="Search here">        </b>        <br>        <br>        <table>            <tr>                <th>Course</th>                <th>Duration</th>                <th>Type</th>            </tr>            <tbody id="geeks">                <tr>                    <td>C++ STL</td>                    <td>1499</td>                    <td>Online Classes                    </td>                </tr>                <tr>                    <td>DSA Foundation</td>                    <td>7999</td>                    <td>Regular Classes</td>                </tr>                <tr>                    <td>Geeks Classes</td>                    <td>10799</td>                    <td>Weekend Classes</td>                </tr>                <tr>                    <td>Placement 100</td>                    <td>9999</td>                    <td>Online Classes</td>                </tr>            </tbody>        </table>          <script>            $(document).ready(function() {                $("#gfg").on("keyup", function() {                    var value = $(this).val().toLowerCase();                    $("#geeks tr").filter(function() {                        $(this).toggle($(this).text()                        .toLowerCase().indexOf(value) > -1)                    });                });            });        </script>  </center>  </body>  </html>
Output:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
jQuery-Misc
Picked
HTML
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to update Node.js and NPM to next version ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?
Types of CSS (Cascading Style Sheet)
HTML | <img> align Attribute
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript