views:

38

answers:

2

using jQuery's .next function I want to show next 2 items. By default it selects only just next item.

I need control, like sometimes I need next 2, sometime next 3

+3  A: 

You can use .nextAll() and a :lt() selector, for example:

.nextAll(':lt(2)') //next 2
.nextAll(':lt(3)') //next 3

Try it out here. If you need it to be programmatic (instead of string concatenation) and change it easily, use .slice() instead:

.nextAll().slice(0, 2) //next 2
.nextAll().slice(0, 3) //next 3

This method allows you to pass as a parameter the number you need a bit easier. You can test it here.

Nick Craver
+1 ohh Nick is back!
Reigel
@Reigel - I know right. I have 10 rep points till I hit the cap for the day and go to sleep, but am scared with Nick awake I won't get them.
Marko
+1  A: 

Use .nextAll instead of .next to get all following siblings, and then use .slice to narrow that down to a range.

hobbs