Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS multiple nth-child without pattern into one selector

I have a table with 5 column under a div with Id. I want to do add click handler by Jquery on the 2, 3 and 5 column. I can do something like this

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

I was wondering is there any other or better way to combine this two nth-child together.

like image 798
Jhankar Mahbub Avatar asked Sep 22 '13 17:09

Jhankar Mahbub


1 Answers

This is probably the best way:

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

There's a shorter way but I don't recommend it because it's really coupled with the DOM (assuming you have only 5 elements like you said in the comments).

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

jsFiddle Demo

like image 158
Itay Avatar answered Oct 02 '22 20:10

Itay