CSS multiple nth-child templateless single selector

I have a table with 5 columns under a div with Id. I want to add a jQuery handler to columns 2, 3, and 5. I can do something like this

$('#myDiv td:nth-child(2), #myDiv td:nth-child(2n+3)').click(function(){ alert('clicked'); }); 

I was wondering if there is another or better way to combine these two nth children together.

+6
source share
1 answer

This is probably the best way:

 $('#myDiv td').filter(':nth-child(2), :nth-child(3), :nth-child(5)')... 

There is a shorter way, but I do not recommend it, because it is really associated with the DOM (if you have only 5 elements, as you said in the comments).

 $('#myDiv td:not(:nth-child(3n - 5))')... 

jsFiddle Demo

+14
source

Source: https://habr.com/ru/post/954364/


All Articles