views:

28

answers:

3

Hello,

I'm using nth-child in my jquery selector to identify columns to modify. I have 5 columns (1-5) and I want to modify only 2,3, and 4. Is there a way to do this with nth-child? Something like:

$('#example tbody tr td:nth-child(2||3||4)')

I have tried several combinations but nothing seems to work. Im pretty new to jQuery selectors any help you could throw at me would be greatly appreciated.

Regards, Joe Chin

+2  A: 
$('#example tbody tr td:not(:last-child, :first-child)')
KennyTM
Thanks this is just what I was looking for.
JoeChin
+2  A: 

You could use .nextUntil() like this:

$('#example tbody tr td:nth-child(1)').nextUntil(':nth-child(5)');​​​​​​​​​​​​​​​​​​​​​​​
patrick dw
Thanks, this also does exactly what I was looking for.
JoeChin
+1  A: 

I would use slice, it is more dynamic.

$('#example tbody tr td').slice(1,4)

Heres the API for the slice method.

Capt Otis
This wouldn't work on a per-row basis. It would be a slice of all the `<td>` elements found.
patrick dw
Thanks, this I've never seen this method before but its going to be pretty handy I'm sure.
JoeChin
@patrick no one here's solution really does haha
Capt Otis
@Capt Otis - That's not correct. The other two answers do work on a per-row basis because the `td` selectors used are some variation of `nth-child`. They both will select the 2nd through 4th columns of every row. Yours doesn't do that because it only grabs a small slice of the entire set. Here's an example: http://jsfiddle.net/m9JH7/
patrick dw
...and here's an example using your solution: http://jsfiddle.net/m9JH7/1/
patrick dw