Created
December 7, 2023 01:36
-
-
Save NeraSnow/1e92b1f878aa19b0fa9de53991986a27 to your computer and use it in GitHub Desktop.
Filtering HTML table
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
// 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