tags:

views:

88

answers:

2

Hello,

i have this:

<div class="selection">
 <a class="current" href="#">1</a>
 <div class="class">text</div>
 <a href="#">2</a>
 <div class="class">text</div>
 <a href="#">4</a>
 <div class="class">text</div>
 <a href="#">5</a>
</div>

i want to select the very next a element after a.current. I did this, but it doenst work.

...

$(".selection a.current").next("a").hide();

i also tried

$(".selection").children("a.current").next("a").hide();

... Arent all the a´s inside .selection siblings and therefore be accesable with the next() selector? I wonder, because it works when i remove the div elements between them.

Would be great if someone knows why this is not working ;).

+3  A: 

Try:

.nextAll("a:first");

And to get the previous:

.prevAll("a:first");

Demo online: http://jsbin.com/ayasa

Jonathan Sampson
...hoooooly snap. I've been wishing there was a site like JS Bin for a while now, but never looked because I never thought it could really happen. Thanks :o
Matchu
@Matchu: It's great for quickly working up jQuery examples :)
Jonathan Sampson
...just now used it in another answer. My life is so much happier now.
Matchu
Congrats :) You'll grow to love it. I believe they're releasing a version 2 here before too long. The main developer is a jQuery Evangelist.
Jonathan Sampson
+2  A: 

From jQuery API browser:

Get the immediately following sibling of each element in the set of matched elements, optionally filtered by a selector.

That's not the immediately following sibling. You could try using nextAll and adding a :first selector:

$(".selection a.current").nextAll("a:first").hide();
Matchu
thank you, i understand now...but one more thing..if i would like to select the previous a element (assuming the last a in the selection had the class current)..how would that work. i tried:$(".selection a.current").prevAll("a:first").hide();shouldnt this also work the other way arround?btw: i am trying to build a list that is selectable with the arrow keys.
Marcel
Try using :last instead of :first in that case. Also, when an answer has answered your question, make sure to click the check mark next to it :)
Matchu