jcubic
jcubic

Reputation: 66650

Find all next elements with specific class

I have html like this:

<ul>
  <li class="foo">One</li>
  <li class="bar">Two</li>
  <li class="bar">Tree</li>
  <li class="bar">Four</li>
  <li class="foo">Five</li>
  <li class="bar">Six</li>
  <li class="bar">Seven</li>
</ul>

and I have JS code like this:

$('.foo').click(function() {
   $(this).allNext('.bar');
});

How can I select all elements that are after .foo and have class bar.

Upvotes: 1

Views: 59

Answers (2)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

beside nextAll may be you want to know about nextUntil() .. it will select all .bar until the next .foo

$('.foo').click(function() {
   $(this).nextUntil('.foo').css('background','red');
});

DEMO

Upvotes: 0

Mario Araque
Mario Araque

Reputation: 4572

You can use nextAll:

$('.foo').click(function() {
   $(this).nextAll('.bar');
});

Upvotes: 2

Related Questions