Is it possible to add a background color to every other row in a table after filtering?

Biz Cook

February 10, 2017

Table row styles are usually straight forward, and generally you’d use CSS. Something similar to this:

tr:nth-child(even){background-color:red;}

But what happens when a user filters the table rows? Once you ‘display:none’ the table rows that don’t qualify for the filter, they disappear, but the visible table rows don’t change their background color accordingly.

This problem is more common than you’d think.

To make sure the table row background colors switch during filtering, call this function on your click event (and also make sure your table ID matches the specified ID in the function):

**You will most likely have to set a default background color for the unfiltered table rows before a click event occurs to cover all your bases. If this is the case, just use the CSS code located at the top of this post!