Created
November 18, 2018 03:43
-
-
Save itsalb3rt/ca42372ebed045c53afb9fd4a143191f to your computer and use it in GitHub Desktop.
Filter a html table using simple javascript, vanilla
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" id="search_input" placeholder="Search..." onkeyup="search_on_table()"> | |
function search_on_table() { | |
var input, filter, table, tr, td, cell, i, j; | |
input = document.getElementById("search_input"); | |
filter = input.value.toUpperCase(); | |
table = document.getElementById("table"); | |
tr = table.getElementsByTagName("tr"); | |
for (i = 1; i < tr.length; i++) { | |
// Hide the row initially. | |
tr[i].style.display = "none"; | |
td = tr[i].getElementsByTagName("td"); | |
for (var j = 0; j < td.length; j++) { | |
cell = tr[i].getElementsByTagName("td")[j]; | |
if (cell) { | |
if (cell.innerHTML.toUpperCase().indexOf(filter) > -1) { | |
tr[i].style.display = ""; | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment