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.

  1. document.getElementById('searchBarInput').addEventListener('input', function(e){
  2. e.preventDefault();
  3. var keyword = (this.value).toLowerCase()
  4. document.querySelectAll('table>tbody tr').forEach( ele => {
  5. var tr_contents = (ele.innerText).toLowerCase()
  6.  
  7. if(tr_contents.includes(keyword) === false && keyword != ""){
  8. ele.style.display = "none";
  9. }else{
  10. ele.style.display = "table-row";
  11. }
  12. })
  13. })

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.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>HTML Table Search Filter - JavaScript</title>
  7. <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" />
  8. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
  9.  
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js" integrity="sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  11. <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>
  12.  
  13. html, body{
  14. height: 100%;
  15. width: 100%;
  16. }
  17. body{
  18. height: 100%;
  19. width: 100%;
  20. }
  21. #search-box-container input.rounded-pill{
  22. border-top-right-radius: 0px !important;
  23. border-bottom-right-radius: 0px !important;
  24. }
  25. #search-box-container span.rounded-pill{
  26. border-top-left-radius: 0px !important;
  27. border-bottom-left-radius: 0px !important;
  28. }
  29. </style>
  30. </head>
  31. <body style="background:#eff3fc">
  32. <nav class="navbar navbar-expand-lg navbar-dark fixed-top" style="background:#495C83">
  33. <div class="container">
  34. <a class="navbar-brand" href="./">HTML Table Search Filter - JavaScript</a>
  35. <div>
  36. <a href="https://sourcecodester.com" class="text-light fw-bolder h6 text-decoration-none" target="_blank">SourceCodester</a>
  37. </div>
  38. </div>
  39. </nav>
  40.  
  41. <main class="container-fluid py-5" style="margin-top:5vh;">
  42. <div class="">
  43. <h2 class="text-center">HTML Table Search Filter</h2>
  44. <hr>
  45. <div class="row my-2">
  46. <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12 mx-auto">
  47. <div class="input-group mb-3" id="search-box-container">
  48. <input type="search" class="form-control rounded-pill" id="search" aria-label="Search" placeholder="Search here...">
  49. <span class="input-group-text rounded-pill"><i class="fa fa-search"></i></span>
  50. </div>
  51. </div>
  52. </div>
  53.  
  54. <div class="card mt-3 rounded-0">
  55. <div class="card-body rounded-0">
  56. <div class="container">
  57. <div class="table-responsive">
  58. <table class="table table-sm table-striped table-bordered" id="sampleTbl">
  59. <tr>
  60. <th>Name</th>
  61. <th>Phone</th>
  62. <th>Email</th>
  63. <th>Address</th>
  64. <th>Postal/Zip</th>
  65. <th>Region</th>
  66. </tr>
  67. </thead>
  68. <tr>
  69. <td>Sarah Everett</td>
  70. <td>1-617-912-7433</td>
  71. <td>P.O. Box 556, 9486 Nisl Rd.</td>
  72. <td>4746</td>
  73. <td>Pernambuco</td>
  74. </tr>
  75. <tr>
  76. <td>Oleg Larsen</td>
  77. <td>(821) 363-8457</td>
  78. <td>987-724 Per Road</td>
  79. <td>475454</td>
  80. <td>Luik</td>
  81. </tr>
  82. <tr>
  83. <td>Benedict White</td>
  84. <td>1-548-350-4667</td>
  85. <td>Ap #989-5168 Fames Av.</td>
  86. <td>113488</td>
  87. <td>Jönköpings län</td>
  88. </tr>
  89. <tr>
  90. <td>Nola Delacruz</td>
  91. <td>1-936-475-8674</td>
  92. <td>896-6289 Lectus. Road</td>
  93. <td>6703</td>
  94. <td>Van</td>
  95. </tr>
  96. <tr>
  97. <td>Vivian Kidd</td>
  98. <td>(757) 153-4279</td>
  99. <td>832-7003 Dapibus Street</td>
  100. <td>28583</td>
  101. <td>Donetsk oblast</td>
  102. </tr>
  103. <tr>
  104. <td>Guinevere Larson</td>
  105. <td>(915) 759-9956</td>
  106. <td>Ap #291-8503 Mi Rd.</td>
  107. <td>9446-5214</td>
  108. <td>Ancash</td>
  109. </tr>
  110. <tr>
  111. <td>Kyle Moody</td>
  112. <td>1-851-573-7506</td>
  113. <td>9419 At, Road</td>
  114. <td>968428</td>
  115. <td>Hà Nội</td>
  116. </tr>
  117. <tr>
  118. <td>Omar Rocha</td>
  119. <td>(562) 410-3857</td>
  120. <td>6292 Lacinia Street</td>
  121. <td>4517</td>
  122. <td>Gyeonggi</td>
  123. </tr>
  124. <tr>
  125. <td>Olga Delgado</td>
  126. <td>(366) 233-5748</td>
  127. <td>P.O. Box 353, 5038 Velit. Av.</td>
  128. <td>47691-731</td>
  129. <td>Gilgit Baltistan</td>
  130. </tr>
  131. <tr>
  132. <td>Merritt Mcgee</td>
  133. <td>1-746-208-7317</td>
  134. <td>808-1124 Aliquam St.</td>
  135. <td>3841</td>
  136. <td>West Region</td>
  137. </tr>
  138. <tr>
  139. <td>Justine Chavez</td>
  140. <td>(888) 536-1286</td>
  141. <td>Ap #217-9908 Sapien, Rd.</td>
  142. <td>554736</td>
  143. <td>Valle d'Aosta</td>
  144. </tr>
  145. <tr>
  146. <td>Quyn Walsh</td>
  147. <td>(822) 642-5281</td>
  148. <td>212-3356 Pharetra Road</td>
  149. <td>4453</td>
  150. <td>North Region</td>
  151. </tr>
  152. <tr>
  153. <td>Reagan England</td>
  154. <td>1-556-795-5815</td>
  155. <td>861 Molestie. Ave</td>
  156. <td>I2 3CH</td>
  157. <td>Manisa</td>
  158. </tr>
  159. <tr>
  160. <td>Piper Trevino</td>
  161. <td>(317) 254-8265</td>
  162. <td>P.O. Box 893, 102 Volutpat Ave</td>
  163. <td>442298</td>
  164. <td>Ivano-Frankivsk oblast</td>
  165. </tr>
  166. <tr>
  167. <td>Vielka Booker</td>
  168. <td>(912) 215-4856</td>
  169. <td>P.O. Box 417, 7460 Erat. Rd.</td>
  170. <td>752788</td>
  171. <td>Lower Austria</td>
  172. </tr>
  173. <tr>
  174. <td>Bethany Morse</td>
  175. <td>(524) 275-7817</td>
  176. <td>Ap #306-1993 Nunc, Rd.</td>
  177. <td>872317</td>
  178. <td>Khyber Pakhtoonkhwa</td>
  179. </tr>
  180. <tr>
  181. <td>Nigel Rodriquez</td>
  182. <td>(514) 539-8835</td>
  183. <td>Ap #253-3593 Eleifend, Rd.</td>
  184. <td>632717</td>
  185. <td>Pembrokeshire</td>
  186. </tr>
  187. <tr>
  188. <td>Neve Wilder</td>
  189. <td>(606) 681-2475</td>
  190. <td>174-9984 Tincidunt Rd.</td>
  191. <td>68-74</td>
  192. <td>Sakhalin Oblast</td>
  193. </tr>
  194. <tr>
  195. <td>Jin Bullock</td>
  196. <td>1-448-486-8858</td>
  197. <td>Ap #368-1205 Eleifend, Rd.</td>
  198. <td>ER3K 9LR</td>
  199. <td>Sokoto</td>
  200. </tr>
  201. <tr>
  202. <td>Knox Brennan</td>
  203. <td>1-437-946-4747</td>
  204. <td>660-8116 Duis Avenue</td>
  205. <td>50573</td>
  206. <td>West-Vlaanderen</td>
  207. </tr>
  208. <tr>
  209. <td>Griffin Guerra</td>
  210. <td>1-532-353-9877</td>
  211. <td>Ap #981-1378 Magna St.</td>
  212. <td>30507</td>
  213. <td>Oaxaca</td>
  214. </tr>
  215. <tr>
  216. <td>Caldwell Alford</td>
  217. <td>(399) 426-9716</td>
  218. <td>233-1455 Vel St.</td>
  219. <td>TN8D 5DO</td>
  220. <td>Guaviare</td>
  221. </tr>
  222. <tr>
  223. <td>Jenette Banks</td>
  224. <td>1-689-333-1734</td>
  225. <td>Ap #930-7347 Natoque St.</td>
  226. <td>40522</td>
  227. <td>Guanajuato</td>
  228. </tr>
  229. <tr>
  230. <td>Keely Golden</td>
  231. <td>1-136-553-0827</td>
  232. <td>1886 Purus St.</td>
  233. <td>876591</td>
  234. <td>Lorraine</td>
  235. </tr>
  236. <tr>
  237. <td>Brittany Jefferson</td>
  238. <td>1-254-510-4464</td>
  239. <td>377 Fermentum Rd.</td>
  240. <td>R5S 0P4</td>
  241. <td>Bryansk Oblast</td>
  242. </tr>
  243. <tr>
  244. <td>Colette Daniel</td>
  245. <td>(623) 311-7056</td>
  246. <td>6464 Magnis Avenue</td>
  247. <td>77209-17877</td>
  248. <td>Balochistan</td>
  249. </tr>
  250. <tr>
  251. <td>Thomas Leon</td>
  252. <td>(637) 570-3881</td>
  253. <td>2008 Nec Rd.</td>
  254. <td>145881</td>
  255. <td>South Island</td>
  256. </tr>
  257. <tr>
  258. <td>Hedley Kirk</td>
  259. <td>1-343-525-8656</td>
  260. <td>Ap #634-6047 Ipsum Rd.</td>
  261. <td>14881</td>
  262. <td>Magallanes y Antártica Chilena</td>
  263. </tr>
  264. <tr>
  265. <td>Nolan Cameron</td>
  266. <td>1-756-806-3531</td>
  267. <td>4644 Tellus. Ave</td>
  268. <td>45884</td>
  269. <td>Jönköpings län</td>
  270. </tr>
  271. <tr>
  272. <td>Arden Pate</td>
  273. <td>1-335-356-1881</td>
  274. <td>P.O. Box 486, 238 Pellentesque, Avenue</td>
  275. <td>65098</td>
  276. <td>Chernivtsi oblast</td>
  277. </tr>
  278. <tr>
  279. <td>Nasim Pace</td>
  280. <td>1-450-872-9114</td>
  281. <td>651-397 Habitant Road</td>
  282. <td>16238</td>
  283. <td>Provence-Alpes-Côte d'Azur</td>
  284. </tr>
  285. <tr>
  286. <td>Gannon Ayala</td>
  287. <td>(736) 171-8754</td>
  288. <td>Ap #861-6258 Consectetuer Street</td>
  289. <td>584451</td>
  290. <td>Mizoram</td>
  291. </tr>
  292. <tr>
  293. <td>Francesca Roman</td>
  294. <td>1-646-242-3444</td>
  295. <td>P.O. Box 733, 8323 Commodo Av.</td>
  296. <td>8599</td>
  297. <td>Ninh Bình</td>
  298. </tr>
  299. <tr>
  300. <td>Isaac Deleon</td>
  301. <td>(629) 282-7534</td>
  302. <td>Ap #170-6859 Quisque Rd.</td>
  303. <td>15026</td>
  304. <td>Møre og Romsdal</td>
  305. </tr>
  306. <tr>
  307. <td>Phillip Sharpe</td>
  308. <td>1-979-794-8245</td>
  309. <td>9163 Amet, Ave</td>
  310. <td>6753-6874</td>
  311. <td>Tasmania</td>
  312. </tr>
  313. <tr>
  314. <td>Xantha Rollins</td>
  315. <td>(688) 304-2656</td>
  316. <td>4230 Sed Street</td>
  317. <td>22066</td>
  318. <td>Styria</td>
  319. </tr>
  320. <tr>
  321. <td>William O'donnell</td>
  322. <td>(315) 447-9643</td>
  323. <td>643-9312 Dis Av.</td>
  324. <td>84807-17053</td>
  325. <td>North Island</td>
  326. </tr>
  327. <tr>
  328. <td>Skyler Travis</td>
  329. <td>(136) 280-5435</td>
  330. <td>553-4413 Consequat St.</td>
  331. <td>q7E 5R9</td>
  332. <td>Cagayan Valley</td>
  333. </tr>
  334. <tr>
  335. <td>Cody Morgan</td>
  336. <td>1-902-254-4752</td>
  337. <td>Ap #929-826 Nec Avenue</td>
  338. <td>R6A 6T7</td>
  339. <td>Sachsen-Anhalt</td>
  340. </tr>
  341. <tr>
  342. <td>Geoffrey Morales</td>
  343. <td>(868) 574-3036</td>
  344. <td>176-4504 Bibendum. Avenue</td>
  345. <td>567041</td>
  346. <td>Bedfordshire</td>
  347. </tr>
  348. <tr>
  349. <td>Lysandra Bond</td>
  350. <td>(747) 608-4473</td>
  351. <td>Ap #649-8684 In Av.</td>
  352. <td>18664</td>
  353. <td>Murcia</td>
  354. </tr>
  355. <tr>
  356. <td>Edan Bridges</td>
  357. <td>1-357-914-8027</td>
  358. <td>500-6626 Scelerisque Street</td>
  359. <td>Y2W 1A7</td>
  360. <td>Catalunya</td>
  361. </tr>
  362. <tr>
  363. <td>Liberty Estes</td>
  364. <td>1-231-159-1113</td>
  365. <td>469-2250 Metus Rd.</td>
  366. <td>P46 6BF</td>
  367. <td>Leinster</td>
  368. </tr>
  369. <tr>
  370. <td>Britanney Stone</td>
  371. <td>(852) 889-0227</td>
  372. <td>751-3959 Orci, Street</td>
  373. <td>32452</td>
  374. <td>Lorraine</td>
  375. </tr>
  376. <tr>
  377. <td>Libby Chavez</td>
  378. <td>(686) 188-2576</td>
  379. <td>537-556 Quisque St.</td>
  380. <td>31344-76127</td>
  381. <td>Dōngběi</td>
  382. </tr>
  383. <tr>
  384. <td>Madison Odom</td>
  385. <td>1-442-211-1767</td>
  386. <td>Ap #579-4446 Suspendisse St.</td>
  387. <td>284283</td>
  388. <td>Alsace</td>
  389. </tr>
  390. <tr>
  391. <td>Matthew Maynard</td>
  392. <td>(218) 148-3643</td>
  393. <td>Ap #778-842 Diam. St.</td>
  394. <td>13417</td>
  395. <td>Lakshadweep</td>
  396. </tr>
  397. <tr>
  398. <td>Nicholas Vinson</td>
  399. <td>1-283-692-7461</td>
  400. <td>827-4740 Nulla Road</td>
  401. <td>1341</td>
  402. <td>Punjab</td>
  403. </tr>
  404. <tr>
  405. <td>Orson Sweeney</td>
  406. <td>(585) 243-8548</td>
  407. <td>Ap #563-9840 Ac, St.</td>
  408. <td>60155</td>
  409. <td>Vienna</td>
  410. </tr>
  411. </tbody>
  412. </table>
  413. </div>
  414. </div>
  415. </div>
  416. </div>
  417. </div>
  418. </main>
  419. <footer class="container-fluid py-3" style="background:#495C83; color:#fff">
  420. <div class="container-fluid my-2">
  421. <div class="text-center">
  422. <b>HTML Table Search Filter &copy; 2023</b>
  423. </div>
  424. </div>
  425. <script src="./script.js"></script>
  426. </body>
  427. </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.

  1. const searchField = document.getElementById('search')
  2. const sampleTblContent = document.querySelector('#sampleTbl tbody')
  3.  
  4. window.onload = function(){
  5. console.log("searchTxt")
  6.  
  7. searchField.addEventListener("input", searchRow)
  8. searchField.addEventListener("change", searchRow)
  9. }
  10.  
  11. window.searchRow = function(e){
  12. e.preventDefault()
  13. var searchTxt = (searchField.value).toLowerCase();
  14. sampleTblContent.querySelectorAll("tr").forEach(el => {
  15. var rowTxt = (el.innerText).toLowerCase();
  16. // console.log(rowTxt.includes(searchTxt))
  17. if(rowTxt.includes(searchTxt) === false && searchTxt !== ""){
  18. // console.log(rowTxt)
  19. el.style.display = 'none'
  20. }else{
  21. el.style.display = 'table-row'
  22. }
  23. })
  24. }

Result

Here's the following snapshot of the result of the page interface.

HTML Table Search Filter

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