
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to create a filter table with JavaScript?
To create a filter table with JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> * { box-sizing: border-box; } .searchField { width: 100%; font-size: 16px; padding: 12px 20px 12px 40px; border: 1px solid #ddd; margin-bottom: 12px; } .birthDayTable { border-collapse: collapse; width: 100%; font-size: 18px; } .birthDayTable th, .birthDayTable td { text-align: left; padding: 12px; } .birthDayTable tr { border-bottom: 4px solid #ddd; } .birthDayTable tr.header, .birthDayTable tr:hover { background-color: #f1f1f1; } </style> </head> <body> <h1>Filter table example</h1> <input type="text" class="searchField" placeholder="Friend Name:" /> <table class="birthDayTable"> <tr class="header"> <th style="width:60%;">Friends</th> <th style="width:40%;">Birthday Month</th> </tr> <tr> <td>Shawn</td> <td>January</td> </tr> <tr> <td>Ron</td> <td>March</td> </tr> <tr> <td>Jack</td> <td>December</td> </tr> <tr> <td>Simon</td> <td>February</td> </tr> <tr> <td>Ricky</td> <td>November</td> </tr> </table> <script> document .querySelector(".searchField") .addEventListener("keyup", searchFunction); function searchFunction() { var input, filter, table, tr, td, i, txtValue; input = document.querySelector(".searchField"); filter = input.value.toUpperCase(); table = document.querySelector(".birthDayTable"); tr = table.getElementsByTagName("tr"); for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[0]; if (td) { txtValue = td.textContent || td.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } </script> </body> </html>
Output
The above code will produce the following output −
On entering something in the search field −
- Related Articles
- How to create a filter list with JavaScript?
- How to create a table caption with JavaScript DOM?
- Filter table with Bootstrap
- How to create a table with a caption?
- How to create a MySQL table with InnoDB engine table?
- How to create a MySQL table with MyISAM engine table?
- How to create a table with date column?
- How to create a MySQL table with indexes?
- How to create a responsive table with CSS?
- How to create a comparison table with CSS?
- How to create a zebra striped table with CSS?
- How to create a responsive pricing table with CSS?
- Filter array with filter() and includes() in JavaScript
- How to create a table with decimal values using JDBC?
- Nested collection filter with JavaScript

Advertisements