Skip to content

Instantly share code, notes, and snippets.

@NeraSnow
Created December 7, 2023 01:36
Show Gist options
  • Save NeraSnow/1e92b1f878aa19b0fa9de53991986a27 to your computer and use it in GitHub Desktop.
Save NeraSnow/1e92b1f878aa19b0fa9de53991986a27 to your computer and use it in GitHub Desktop.
Filtering HTML table
// Replace 'table' with the actual ID or class of your HTML table
var table = document.querySelector('table');
// Replace 1 with the index of the column you want to filter (indexing starts from 0)
var columnIndex = 3;
// Replace 'example' with the text you want to filter
var searchText = 'example';
for (var i = 1, row; row = table.rows[i]; i++) {
var cellText = row.cells[columnIndex].textContent || row.cells[columnIndex].innerText;
if (cellText.includes(searchText)) {
// If the cell text contains the search text, display the row
row.style.display = '';
} else {
// If the cell text does not contain the search text, hide the row
row.style.display = 'none';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment