Cybal Rankine
Cybal Rankine

Reputation: 5

jQuery next() didn't work in my case

I want to traverse from start to target

<a class="start">
   <span></span>
</a>
<div class='target'></div>

and I use this line

$(this).next('.target')

$(this) refers to span. am I traverse correctly?

Upvotes: 0

Views: 42

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

If this is the span, it has no siblings, so next will not work. To get to the .target div, you need to go up a level in the DOM, then use next. Try this;

$(this).parent().next(); // = the `.target` div

Alternatively you can use closest() with a selector:

$(this).closest('a').next();

Upvotes: 5

Related Questions