I’m trying to use jquery to set the class of all visible elements of pattern (3n+1), but it doesn’t seem to be selecting the way that I anticipated. I’m wondering if my syntax is wrong. Here’s my code:
$('.filterElement:visible:nth-child(3n+1)').addClass('firstInRow');
I was following the method suggested here:
Make css nth-child() only affect visible
Is there a way to do this with pure css?
nth-child doesn’t seem to play well when there is hidden items…period. You can see an example here: http://jsfiddle.net/anAgent/FzBWn/. Click on the “Test 2” button to run the selector against the collection of rows that contain hidden values. The jQuery code below is how you can select what you need.
CSS
HTML
jQuery
Get all visible items and then find if it’s the third one in the list.