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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With