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.
- How to create table with 100% width, with vertical scroll inside table body in HTML ?
- What is RTS(Real Time Streaming)?
- HTML | DOM Style filter Property
- HTML | DOM Table Object
- HTML | DOM Table tBodies Collection
- HTML | <table> width Attribute
- HTML | DOM Table tFoot Property
- HTML | DOM Table caption Property
- HTML | <table> frame Attribute
- HTML <table> align Attribute
- HTML | <table> rules Attribute
- HTML | DOM console.table() Method
- HTML | <table> border Attribute
- HTML | <table> cellspacing Attribute
- HTML | <table> summary Attribute
- HTML | <table> cellpadding Attribute
- HTML | DOM Table deleteRow( ) Method
- HTML | DOM Table createTHead() Method
- HTML | DOM Table insertRow( ) Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.