I can't figure out the following; I have a simple JS script that filters/hides rows of the table based on a search value. How can I search two columns td0 and td1 at the same time?
function myFunction() {
// Declare variables
var input, filter, table, tr, td0,td1, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td0 = tr[i].getElementsByTagName("td")[0];
td1 = tr[i].getElementsByTagName("td")[1];
// search first column
if (td0) {
txtValue = td0.textContent || td0.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
// search second column
if (td1) {
txtValue = td1.textContent || td1.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
I'm sure that this is not the right approach though. I am trying to search both td0 and td1 at the same time. Any help would be greatly appreciate it!