Skip to content

Instantly share code, notes, and snippets.

@itsalb3rt
Created November 18, 2018 03:43
Show Gist options
  • Save itsalb3rt/ca42372ebed045c53afb9fd4a143191f to your computer and use it in GitHub Desktop.
Save itsalb3rt/ca42372ebed045c53afb9fd4a143191f to your computer and use it in GitHub Desktop.
Filter a html table using simple javascript, vanilla
<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