• We just launched and are currently in beta. Join us as we build and grow the community.

HTML Table Search Filter using Pure JavaScript Tutorial

jkentnew

Content Velocity Specialist
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
164
Likes
76
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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. if

    (

    tr_contents.includes

    (

    keyword)

    ===

    false

    &&

    keyword !=

    ""

    )

    {
  7. ele.style

    .display

    =

    "none"

    ;
  8. }

    else

    {
  9. ele.style

    .display

    =

    "table-row"

    ;
  10. }
  11. }

    )
  12. }

    )

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. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    HTML Table Search Filter - JavaScript</

    title

    >
  8. <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"

    /

    >
  9. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"

    integrity=

    "sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"

    crossorigin=

    "anonymous"

    >

  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. <style

    >
  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. <main class

    =

    "container-fluid py-5"

    style

    =

    "margin-top:5vh;"

    >
  41. <div

    class

    =

    ""

    >
  42. <h2

    class

    =

    "text-center"

    >

    HTML Table Search Filter</

    h2

    >
  43. <hr

    >
  44. <div

    class

    =

    "row my-2"

    >
  45. <div

    class

    =

    "col-lg-4 col-md-6 col-sm-12 col-xs-12 mx-auto"

    >
  46. <div

    class

    =

    "input-group mb-3"

    id

    =

    "search-box-container"

    >
  47. <input

    type

    =

    "search"

    class

    =

    "form-control rounded-pill"

    id

    =

    "search"

    aria-label

    =

    "Search"

    placeholder

    =

    "Search here..."

    >
  48. <span

    class

    =

    "input-group-text rounded-pill"

    ><i

    class

    =

    "fa fa-search"

    ></

    i

    ></

    span

    >
  49. </

    div

    >
  50. </

    div

    >
  51. </

    div

    >

  52. <div

    class

    =

    "card mt-3 rounded-0"

    >
  53. <div

    class

    =

    "card-body rounded-0"

    >
  54. <div

    class

    =

    "container"

    >
  55. <div

    class

    =

    "table-responsive"

    >
  56. <table

    class

    =

    "table table-sm table-striped table-bordered"

    id

    =

    "sampleTbl"

    >
  57. <thead

    >
  58. <tr

    >
  59. <th

    >

    Name</

    th

    >
  60. <th

    >

    Phone</

    th

    >
  61. <th

    >

    Email</

    th

    >
  62. <th

    >

    Address</

    th

    >
  63. <th

    >

    Postal/Zip</

    th

    >
  64. <th

    >

    Region</

    th

    >
  65. </

    tr

    >
  66. </

    thead

    >
  67. <tbody

    >
  68. <tr

    >
  69. <td

    >

    Sarah Everett</

    td

    >
  70. <td

    >

    1-617-912-7433</

    td

    >
  71. <td

    >

    [email protected]</

    td

    >
  72. <td

    >

    P.O. Box 556, 9486 Nisl Rd.</

    td

    >
  73. <td

    >

    4746</

    td

    >
  74. <td

    >

    Pernambuco</

    td

    >
  75. </

    tr

    >
  76. <tr

    >
  77. <td

    >

    Oleg Larsen</

    td

    >
  78. <td

    >

    (821) 363-8457</

    td

    >
  79. <td

    >

    [email protected]</

    td

    >
  80. <td

    >

    987-724 Per Road</

    td

    >
  81. <td

    >

    475454</

    td

    >
  82. <td

    >

    Luik</

    td

    >
  83. </

    tr

    >
  84. <tr

    >
  85. <td

    >

    Benedict White</

    td

    >
  86. <td

    >

    1-548-350-4667</

    td

    >
  87. <td

    >

    [email protected]</

    td

    >
  88. <td

    >

    Ap #989-5168 Fames Av.</

    td

    >
  89. <td

    >

    113488</

    td

    >
  90. <td

    >

    Jönköpings län</

    td

    >
  91. </

    tr

    >
  92. <tr

    >
  93. <td

    >

    Nola Delacruz</

    td

    >
  94. <td

    >

    1-936-475-8674</

    td

    >
  95. <td

    >

    [email protected]</

    td

    >
  96. <td

    >

    896-6289 Lectus. Road</

    td

    >
  97. <td

    >

    6703</

    td

    >
  98. <td

    >

    Van</

    td

    >
  99. </

    tr

    >
  100. <tr

    >
  101. <td

    >

    Vivian Kidd</

    td

    >
  102. <td

    >

    (757) 153-4279</

    td

    >
  103. <td

    >

    [email protected]</

    td

    >
  104. <td

    >

    832-7003 Dapibus Street</

    td

    >
  105. <td

    >

    28583</

    td

    >
  106. <td

    >

    Donetsk oblast</

    td

    >
  107. </

    tr

    >
  108. <tr

    >
  109. <td

    >

    Guinevere Larson</

    td

    >
  110. <td

    >

    (915) 759-9956</

    td

    >
  111. <td

    >

    [email protected]</

    td

    >
  112. <td

    >

    Ap #291-8503 Mi Rd.</

    td

    >
  113. <td

    >

    9446-5214</

    td

    >
  114. <td

    >

    Ancash</

    td

    >
  115. </

    tr

    >
  116. <tr

    >
  117. <td

    >

    Kyle Moody</

    td

    >
  118. <td

    >

    1-851-573-7506</

    td

    >
  119. <td

    >

    [email protected]</

    td

    >
  120. <td

    >

    9419 At, Road</

    td

    >
  121. <td

    >

    968428</

    td

    >
  122. <td

    >

    Hà Nội</

    td

    >
  123. </

    tr

    >
  124. <tr

    >
  125. <td

    >

    Omar Rocha</

    td

    >
  126. <td

    >

    (562) 410-3857</

    td

    >
  127. <td

    >

    [email protected]</

    td

    >
  128. <td

    >

    6292 Lacinia Street</

    td

    >
  129. <td

    >

    4517</

    td

    >
  130. <td

    >

    Gyeonggi</

    td

    >
  131. </

    tr

    >
  132. <tr

    >
  133. <td

    >

    Olga Delgado</

    td

    >
  134. <td

    >

    (366) 233-5748</

    td

    >
  135. <td

    >

    [email protected]</

    td

    >
  136. <td

    >

    P.O. Box 353, 5038 Velit. Av.</

    td

    >
  137. <td

    >

    47691-731</

    td

    >
  138. <td

    >

    Gilgit Baltistan</

    td

    >
  139. </

    tr

    >
  140. <tr

    >
  141. <td

    >

    Merritt Mcgee</

    td

    >
  142. <td

    >

    1-746-208-7317</

    td

    >
  143. <td

    >

    [email protected]</

    td

    >
  144. <td

    >

    808-1124 Aliquam St.</

    td

    >
  145. <td

    >

    3841</

    td

    >
  146. <td

    >

    West Region</

    td

    >
  147. </

    tr

    >
  148. <tr

    >
  149. <td

    >

    Justine Chavez</

    td

    >
  150. <td

    >

    (888) 536-1286</

    td

    >
  151. <td

    >

    [email protected]</

    td

    >
  152. <td

    >

    Ap #217-9908 Sapien, Rd.</

    td

    >
  153. <td

    >

    554736</

    td

    >
  154. <td

    >

    Valle d'Aosta</

    td

    >
  155. </

    tr

    >
  156. <tr

    >
  157. <td

    >

    Quyn Walsh</

    td

    >
  158. <td

    >

    (822) 642-5281</

    td

    >
  159. <td

    >

    [email protected]</

    td

    >
  160. <td

    >

    212-3356 Pharetra Road</

    td

    >
  161. <td

    >

    4453</

    td

    >
  162. <td

    >

    North Region</

    td

    >
  163. </

    tr

    >
  164. <tr

    >
  165. <td

    >

    Reagan England</

    td

    >
  166. <td

    >

    1-556-795-5815</

    td

    >
  167. <td

    >

    [email protected]</

    td

    >
  168. <td

    >

    861 Molestie. Ave</

    td

    >
  169. <td

    >

    I2 3CH</

    td

    >
  170. <td

    >

    Manisa</

    td

    >
  171. </

    tr

    >
  172. <tr

    >
  173. <td

    >

    Piper Trevino</

    td

    >
  174. <td

    >

    (317) 254-8265</

    td

    >
  175. <td

    >

    [email protected]</

    td

    >
  176. <td

    >

    P.O. Box 893, 102 Volutpat Ave</

    td

    >
  177. <td

    >

    442298</

    td

    >
  178. <td

    >

    Ivano-Frankivsk oblast</

    td

    >
  179. </

    tr

    >
  180. <tr

    >
  181. <td

    >

    Vielka Booker</

    td

    >
  182. <td

    >

    (912) 215-4856</

    td

    >
  183. <td

    >

    [email protected]</

    td

    >
  184. <td

    >

    P.O. Box 417, 7460 Erat. Rd.</

    td

    >
  185. <td

    >

    752788</

    td

    >
  186. <td

    >

    Lower Austria</

    td

    >
  187. </

    tr

    >
  188. <tr

    >
  189. <td

    >

    Bethany Morse</

    td

    >
  190. <td

    >

    (524) 275-7817</

    td

    >
  191. <td

    >

    [email protected]</

    td

    >
  192. <td

    >

    Ap #306-1993 Nunc, Rd.</

    td

    >
  193. <td

    >

    872317</

    td

    >
  194. <td

    >

    Khyber Pakhtoonkhwa</

    td

    >
  195. </

    tr

    >
  196. <tr

    >
  197. <td

    >

    Nigel Rodriquez</

    td

    >
  198. <td

    >

    (514) 539-8835</

    td

    >
  199. <td

    >

    [email protected]</

    td

    >
  200. <td

    >

    Ap #253-3593 Eleifend, Rd.</

    td

    >
  201. <td

    >

    632717</

    td

    >
  202. <td

    >

    Pembrokeshire</

    td

    >
  203. </

    tr

    >
  204. <tr

    >
  205. <td

    >

    Neve Wilder</

    td

    >
  206. <td

    >

    (606) 681-2475</

    td

    >
  207. <td

    >

    [email protected]</

    td

    >
  208. <td

    >

    174-9984 Tincidunt Rd.</

    td

    >
  209. <td

    >

    68-74</

    td

    >
  210. <td

    >

    Sakhalin Oblast</

    td

    >
  211. </

    tr

    >
  212. <tr

    >
  213. <td

    >

    Jin Bullock</

    td

    >
  214. <td

    >

    1-448-486-8858</

    td

    >
  215. <td

    >

    [email protected]</

    td

    >
  216. <td

    >

    Ap #368-1205 Eleifend, Rd.</

    td

    >
  217. <td

    >

    ER3K 9LR</

    td

    >
  218. <td

    >

    Sokoto</

    td

    >
  219. </

    tr

    >
  220. <tr

    >
  221. <td

    >

    Knox Brennan</

    td

    >
  222. <td

    >

    1-437-946-4747</

    td

    >
  223. <td

    >

    [email protected]</

    td

    >
  224. <td

    >

    660-8116 Duis Avenue</

    td

    >
  225. <td

    >

    50573</

    td

    >
  226. <td

    >

    West-Vlaanderen</

    td

    >
  227. </

    tr

    >
  228. <tr

    >
  229. <td

    >

    Griffin Guerra</

    td

    >
  230. <td

    >

    1-532-353-9877</

    td

    >
  231. <td

    >

    [email protected]</

    td

    >
  232. <td

    >

    Ap #981-1378 Magna St.</

    td

    >
  233. <td

    >

    30507</

    td

    >
  234. <td

    >

    Oaxaca</

    td

    >
  235. </

    tr

    >
  236. <tr

    >
  237. <td

    >

    Caldwell Alford</

    td

    >
  238. <td

    >

    (399) 426-9716</

    td

    >
  239. <td

    >

    [email protected]</

    td

    >
  240. <td

    >

    233-1455 Vel St.</

    td

    >
  241. <td

    >

    TN8D 5DO</

    td

    >
  242. <td

    >

    Guaviare</

    td

    >
  243. </

    tr

    >
  244. <tr

    >
  245. <td

    >

    Jenette Banks</

    td

    >
  246. <td

    >

    1-689-333-1734</

    td

    >
  247. <td

    >

    [email protected]</

    td

    >
  248. <td

    >

    Ap #930-7347 Natoque St.</

    td

    >
  249. <td

    >

    40522</

    td

    >
  250. <td

    >

    Guanajuato</

    td

    >
  251. </

    tr

    >
  252. <tr

    >
  253. <td

    >

    Keely Golden</

    td

    >
  254. <td

    >

    1-136-553-0827</

    td

    >
  255. <td

    >

    [email protected]</

    td

    >
  256. <td

    >

    1886 Purus St.</

    td

    >
  257. <td

    >

    876591</

    td

    >
  258. <td

    >

    Lorraine</

    td

    >
  259. </

    tr

    >
  260. <tr

    >
  261. <td

    >

    Brittany Jefferson</

    td

    >
  262. <td

    >

    1-254-510-4464</

    td

    >
  263. <td

    >

    [email protected]</

    td

    >
  264. <td

    >

    377 Fermentum Rd.</

    td

    >
  265. <td

    >

    R5S 0P4</

    td

    >
  266. <td

    >

    Bryansk Oblast</

    td

    >
  267. </

    tr

    >
  268. <tr

    >
  269. <td

    >

    Colette Daniel</

    td

    >
  270. <td

    >

    (623) 311-7056</

    td

    >
  271. <td

    >

    [email protected]</

    td

    >
  272. <td

    >

    6464 Magnis Avenue</

    td

    >
  273. <td

    >

    77209-17877</

    td

    >
  274. <td

    >

    Balochistan</

    td

    >
  275. </

    tr

    >
  276. <tr

    >
  277. <td

    >

    Thomas Leon</

    td

    >
  278. <td

    >

    (637) 570-3881</

    td

    >
  279. <td

    >

    [email protected]</

    td

    >
  280. <td

    >

    2008 Nec Rd.</

    td

    >
  281. <td

    >

    145881</

    td

    >
  282. <td

    >

    South Island</

    td

    >
  283. </

    tr

    >
  284. <tr

    >
  285. <td

    >

    Hedley Kirk</

    td

    >
  286. <td

    >

    1-343-525-8656</

    td

    >
  287. <td

    >

    [email protected]</

    td

    >
  288. <td

    >

    Ap #634-6047 Ipsum Rd.</

    td

    >
  289. <td

    >

    14881</

    td

    >
  290. <td

    >

    Magallanes y Antártica Chilena</

    td

    >
  291. </

    tr

    >
  292. <tr

    >
  293. <td

    >

    Nolan Cameron</

    td

    >
  294. <td

    >

    1-756-806-3531</

    td

    >
  295. <td

    >

    [email protected]</

    td

    >
  296. <td

    >

    4644 Tellus. Ave</

    td

    >
  297. <td

    >

    45884</

    td

    >
  298. <td

    >

    Jönköpings län</

    td

    >
  299. </

    tr

    >
  300. <tr

    >
  301. <td

    >

    Arden Pate</

    td

    >
  302. <td

    >

    1-335-356-1881</

    td

    >
  303. <td

    >

    [email protected]</

    td

    >
  304. <td

    >

    P.O. Box 486, 238 Pellentesque, Avenue</

    td

    >
  305. <td

    >

    65098</

    td

    >
  306. <td

    >

    Chernivtsi oblast</

    td

    >
  307. </

    tr

    >
  308. <tr

    >
  309. <td

    >

    Nasim Pace</

    td

    >
  310. <td

    >

    1-450-872-9114</

    td

    >
  311. <td

    >

    [email protected]</

    td

    >
  312. <td

    >

    651-397 Habitant Road</

    td

    >
  313. <td

    >

    16238</

    td

    >
  314. <td

    >

    Provence-Alpes-Côte d'Azur</

    td

    >
  315. </

    tr

    >
  316. <tr

    >
  317. <td

    >

    Gannon Ayala</

    td

    >
  318. <td

    >

    (736) 171-8754</

    td

    >
  319. <td

    >

    [email protected]</

    td

    >
  320. <td

    >

    Ap #861-6258 Consectetuer Street</

    td

    >
  321. <td

    >

    584451</

    td

    >
  322. <td

    >

    Mizoram</

    td

    >
  323. </

    tr

    >
  324. <tr

    >
  325. <td

    >

    Francesca Roman</

    td

    >
  326. <td

    >

    1-646-242-3444</

    td

    >
  327. <td

    >

    [email protected]</

    td

    >
  328. <td

    >

    P.O. Box 733, 8323 Commodo Av.</

    td

    >
  329. <td

    >

    8599</

    td

    >
  330. <td

    >

    Ninh Bình</

    td

    >
  331. </

    tr

    >
  332. <tr

    >
  333. <td

    >

    Isaac Deleon</

    td

    >
  334. <td

    >

    (629) 282-7534</

    td

    >
  335. <td

    >

    [email protected]</

    td

    >
  336. <td

    >

    Ap #170-6859 Quisque Rd.</

    td

    >
  337. <td

    >

    15026</

    td

    >
  338. <td

    >

    Møre og Romsdal</

    td

    >
  339. </

    tr

    >
  340. <tr

    >
  341. <td

    >

    Phillip Sharpe</

    td

    >
  342. <td

    >

    1-979-794-8245</

    td

    >
  343. <td

    >

    [email protected]</

    td

    >
  344. <td

    >

    9163 Amet, Ave</

    td

    >
  345. <td

    >

    6753-6874</

    td

    >
  346. <td

    >

    Tasmania</

    td

    >
  347. </

    tr

    >
  348. <tr

    >
  349. <td

    >

    Xantha Rollins</

    td

    >
  350. <td

    >

    (688) 304-2656</

    td

    >
  351. <td

    >

    [email protected]</

    td

    >
  352. <td

    >

    4230 Sed Street</

    td

    >
  353. <td

    >

    22066</

    td

    >
  354. <td

    >

    Styria</

    td

    >
  355. </

    tr

    >
  356. <tr

    >
  357. <td

    >

    William O'donnell</

    td

    >
  358. <td

    >

    (315) 447-9643</

    td

    >
  359. <td

    >

    [email protected]</

    td

    >
  360. <td

    >

    643-9312 Dis Av.</

    td

    >
  361. <td

    >

    84807-17053</

    td

    >
  362. <td

    >

    North Island</

    td

    >
  363. </

    tr

    >
  364. <tr

    >
  365. <td

    >

    Skyler Travis</

    td

    >
  366. <td

    >

    (136) 280-5435</

    td

    >
  367. <td

    >

    [email protected]</

    td

    >
  368. <td

    >

    553-4413 Consequat St.</

    td

    >
  369. <td

    >

    q7E 5R9</

    td

    >
  370. <td

    >

    Cagayan Valley</

    td

    >
  371. </

    tr

    >
  372. <tr

    >
  373. <td

    >

    Cody Morgan</

    td

    >
  374. <td

    >

    1-902-254-4752</

    td

    >
  375. <td

    >

    [email protected]</

    td

    >
  376. <td

    >

    Ap #929-826 Nec Avenue</

    td

    >
  377. <td

    >

    R6A 6T7</

    td

    >
  378. <td

    >

    Sachsen-Anhalt</

    td

    >
  379. </

    tr

    >
  380. <tr

    >
  381. <td

    >

    Geoffrey Morales</

    td

    >
  382. <td

    >

    (868) 574-3036</

    td

    >
  383. <td

    >

    [email protected]</

    td

    >
  384. <td

    >

    176-4504 Bibendum. Avenue</

    td

    >
  385. <td

    >

    567041</

    td

    >
  386. <td

    >

    Bedfordshire</

    td

    >
  387. </

    tr

    >
  388. <tr

    >
  389. <td

    >

    Lysandra Bond</

    td

    >
  390. <td

    >

    (747) 608-4473</

    td

    >
  391. <td

    >

    [email protected]</

    td

    >
  392. <td

    >

    Ap #649-8684 In Av.</

    td

    >
  393. <td

    >

    18664</

    td

    >
  394. <td

    >

    Murcia</

    td

    >
  395. </

    tr

    >
  396. <tr

    >
  397. <td

    >

    Edan Bridges</

    td

    >
  398. <td

    >

    1-357-914-8027</

    td

    >
  399. <td

    >

    [email protected]</

    td

    >
  400. <td

    >

    500-6626 Scelerisque Street</

    td

    >
  401. <td

    >

    Y2W 1A7</

    td

    >
  402. <td

    >

    Catalunya</

    td

    >
  403. </

    tr

    >
  404. <tr

    >
  405. <td

    >

    Liberty Estes</

    td

    >
  406. <td

    >

    1-231-159-1113</

    td

    >
  407. <td

    >

    [email protected]</

    td

    >
  408. <td

    >

    469-2250 Metus Rd.</

    td

    >
  409. <td

    >

    P46 6BF</

    td

    >
  410. <td

    >

    Leinster</

    td

    >
  411. </

    tr

    >
  412. <tr

    >
  413. <td

    >

    Britanney Stone</

    td

    >
  414. <td

    >

    (852) 889-0227</

    td

    >
  415. <td

    >

    [email protected]</

    td

    >
  416. <td

    >

    751-3959 Orci, Street</

    td

    >
  417. <td

    >

    32452</

    td

    >
  418. <td

    >

    Lorraine</

    td

    >
  419. </

    tr

    >
  420. <tr

    >
  421. <td

    >

    Libby Chavez</

    td

    >
  422. <td

    >

    (686) 188-2576</

    td

    >
  423. <td

    >

    [email protected]</

    td

    >
  424. <td

    >

    537-556 Quisque St.</

    td

    >
  425. <td

    >

    31344-76127</

    td

    >
  426. <td

    >

    Dōngběi</

    td

    >
  427. </

    tr

    >
  428. <tr

    >
  429. <td

    >

    Madison Odom</

    td

    >
  430. <td

    >

    1-442-211-1767</

    td

    >
  431. <td

    >

    [email protected]</

    td

    >
  432. <td

    >

    Ap #579-4446 Suspendisse St.</

    td

    >
  433. <td

    >

    284283</

    td

    >
  434. <td

    >

    Alsace</

    td

    >
  435. </

    tr

    >
  436. <tr

    >
  437. <td

    >

    Matthew Maynard</

    td

    >
  438. <td

    >

    (218) 148-3643</

    td

    >
  439. <td

    >

    [email protected]</

    td

    >
  440. <td

    >

    Ap #778-842 Diam. St.</

    td

    >
  441. <td

    >

    13417</

    td

    >
  442. <td

    >

    Lakshadweep</

    td

    >
  443. </

    tr

    >
  444. <tr

    >
  445. <td

    >

    Nicholas Vinson</

    td

    >
  446. <td

    >

    1-283-692-7461</

    td

    >
  447. <td

    >

    [email protected]</

    td

    >
  448. <td

    >

    827-4740 Nulla Road</

    td

    >
  449. <td

    >

    1341</

    td

    >
  450. <td

    >

    Punjab</

    td

    >
  451. </

    tr

    >
  452. <tr

    >
  453. <td

    >

    Orson Sweeney</

    td

    >
  454. <td

    >

    (585) 243-8548</

    td

    >
  455. <td

    >

    [email protected]</

    td

    >
  456. <td

    >

    Ap #563-9840 Ac, St.</

    td

    >
  457. <td

    >

    60155</

    td

    >
  458. <td

    >

    Vienna</

    td

    >
  459. </

    tr

    >
  460. </

    tbody

    >
  461. </

    table

    >
  462. </

    div

    >
  463. </

    div

    >
  464. </

    div

    >
  465. </

    div

    >
  466. </

    div

    >
  467. </

    main>
  468. <footer

    class

    =

    "container-fluid py-3"

    style

    =

    "background:#495C83; color:#fff"

    >
  469. <div

    class

    =

    "container-fluid my-2"

    >
  470. <div

    class

    =

    "text-center"

    >
  471. <b

    >

    HTML Table Search Filter &copy;

    2023</

    b

    >
  472. </

    div

    >
  473. </

    div

    >
  474. </

    footer

    >
  475. <script

    src

    =

    "./script.js"

    ></

    script

    >
  476. </

    body

    >
  477. </

    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. window.onload

    =

    function

    (

    )

    {
  4. console.log

    (

    "searchTxt"

    )

  5. searchField.addEventListener

    (

    "input"

    ,

    searchRow)
  6. searchField.addEventListener

    (

    "change"

    ,

    searchRow)
  7. }

  8. window.searchRow

    =

    function

    (

    e)

    {
  9. e.preventDefault

    (

    )
  10. var

    searchTxt =

    (

    searchField.value

    )

    .toLowerCase

    (

    )

    ;
  11. sampleTblContent.querySelectorAll

    (

    "tr"

    )

    .forEach

    (

    el =>

    {
  12. var

    rowTxt =

    (

    el.innerText

    )

    .toLowerCase

    (

    )

    ;
  13. // console.log(rowTxt.includes(searchTxt))
  14. if

    (

    rowTxt.includes

    (

    searchTxt)

    ===

    false

    &&

    searchTxt !==

    ""

    )

    {
  15. // console.log(rowTxt)
  16. el.style

    .display

    =

    'none'
  17. }

    else

    {
  18. el.style

    .display

    =

    'table-row'
  19. }
  20. }

    )
  21. }

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 =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top