HTML Table Search Filter using Pure JavaScript Tutorial
In this tutorial, you will learn how to create a Search Filter Bar for HTML Table using JavaScript. This tutorial aims to provide IT/CS students and new programmers with a reference to learn about using JavaScript to develop creative web applications. Here, snippets are provided and a simple working application that demonstrates the main objectives of this tutorial is provided and is free to download.
Why do we need to Implement a Search Bar for HTML Table?
A Search Bar/Search Filter for HTML Table is not always a requirement in a web application. The developers usually implement this feature along with the pagination feature to easily navigate and find some data from the HTML Table. This feature allows the user to easily find the table row that contains the keyword entered on the search bar.
How to Implement Search Filter in HTML Table using JavaScript?
JavaScript comes with various built-in and useful methods and APIs. JS has a method called addEventListener() and inludes() that can be used to make the Search Filter functional. Using the addEventListener(), we can identify if the search bar has changed so we can trigger the search functions. The includes() method will become handy for identifying if the table row contains the keyword entered in the search box. Check out the following snippet to have a better idea of using these JS methods to implement the search filter for HTML Table functionality.
- document.getElementById('searchBarInput').addEventListener('input', function(e){
- e.preventDefault();
- var keyword = (this.value).toLowerCase()
- document.querySelectAll('table>tbody tr').forEach( ele => {
- var tr_contents = (ele.innerText).toLowerCase()
- if(tr_contents.includes(keyword) === false && keyword != ""){
- ele.style.display = "none";
- }else{
- ele.style.display = "table-row";
- }
- })
- })
The script above hides the table row for those contents doesn't have or contain the keyword entered on the search input. The function will be triggered each time the search input changes to its value.
Example
Here's an example web application source code that demonstrates the use of JavaScript to implement the Search Filter for HTML Table.
Step 1: Creating the Page Interface
The following script is an HTML file that contains the script of the application page with a table with static data and a search bar element.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
- <style>
- html, body{
- height: 100%;
- width: 100%;
- }
- body{
- height: 100%;
- width: 100%;
- }
- #search-box-container input.rounded-pill{
- border-top-right-radius: 0px !important;
- border-bottom-right-radius: 0px !important;
- }
- #search-box-container span.rounded-pill{
- border-top-left-radius: 0px !important;
- border-bottom-left-radius: 0px !important;
- }
- </style>
- </head>
- <body style="background:#eff3fc">
- <nav class="navbar navbar-expand-lg navbar-dark fixed-top" style="background:#495C83">
- <div class="container">
- <div>
- </div>
- </div>
- </nav>
- <main class="container-fluid py-5" style="margin-top:5vh;">
- <div class="">
- <hr>
- <div class="row my-2">
- <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12 mx-auto">
- <div class="input-group mb-3" id="search-box-container">
- <input type="search" class="form-control rounded-pill" id="search" aria-label="Search" placeholder="Search here...">
- </div>
- </div>
- </div>
- <div class="card mt-3 rounded-0">
- <div class="card-body rounded-0">
- <div class="container">
- <div class="table-responsive">
- <table class="table table-sm table-striped table-bordered" id="sampleTbl">
- <thead>
- <tr>
- </tr>
- </thead>
- <tbody>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- <tr>
- </tr>
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </div>
- </div>
- </main>
- <footer class="container-fluid py-3" style="background:#495C83; color:#fff">
- <div class="container-fluid my-2">
- <div class="text-center">
- </div>
- </div>
- </footer>
- </body>
- </html>
Step 2: Creating the JavaScript
The following script is the main script that makes the search filter feature functional. This file is included on the page interface script.
- const searchField = document.getElementById('search')
- const sampleTblContent = document.querySelector('#sampleTbl tbody')
- window.onload = function(){
- console.log("searchTxt")
- searchField.addEventListener("input", searchRow)
- searchField.addEventListener("change", searchRow)
- }
- window.searchRow = function(e){
- e.preventDefault()
- var searchTxt = (searchField.value).toLowerCase();
- sampleTblContent.querySelectorAll("tr").forEach(el => {
- var rowTxt = (el.innerText).toLowerCase();
- // console.log(rowTxt.includes(searchTxt))
- if(rowTxt.includes(searchTxt) === false && searchTxt !== ""){
- // console.log(rowTxt)
- el.style.display = 'none'
- }else{
- el.style.display = 'table-row'
- }
- })
- }
Result
Here's the following snapshot of the result of the page interface.
That's it! You can now test the application on your end and check if it works properly and achieves the main objective of this tutorial. I have provided the source code zip file on this website and is free to download. The download button is located below this article.
That's the end of this tutorial. I hope this HTML Table Search Filter using JavaScript Tutorial will help you with what you are looking for and that you'll find this useful for your current and future web applications.
Explore more on this web application for more Tutorials and Free Source Codes.
Happy Coding =)
Add new comment
- 783 views