Skip to content
Advertisement

Table filter/search for two columns

Í’m trying to make this search/filter function filter by looking at data in col0 or col1. What I’m trying to change the JS, to look at td = tr[i].getElementsByTagName("td")[0]; and td = tr[i].getElementsByTagName("td")[1];, but can’t make it work.

Code working for searching on Name (not country):

JavaScript
JavaScript
JavaScript

Advertisement

Answer

You can grab the textContent of both the columns and then display the rows where the textContent contains the filterText.

I’ve also made the following changes to your code:

JavaScript
JavaScript
JavaScript

If there are more than two columns and you want to apply filter based on all columns, then you can use Array.prototype.some and check if any of the columns contain the search text.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement