Vincent Duprez
Vincent Duprez

Reputation: 3882

How to select all following elements with class in jquery

How can I select all following elements from my selector, stopping when the next element is not part of it and ignoring the further elements?

example:

<div class="primary"></div>
  <div class="sub"></div>
  <div class="sub"></div>
  <div class="sub"></div>
<div class="primary"></div>
 <div class="sub"></div>
<div class="primary"></div>
 <div class="sub"></div>
 <div class="sub"></div>

now in js / jquery:

$('.primary').click(function()
{
$(this)....  //acces to all following elements with class "sub"
});

so when clicking on the first primary div, I can affect the 3 following "sub" divs I know I could do some kind of .next() in a while, but this seams inefficient.

Upvotes: 0

Views: 73

Answers (2)

TheDesigner
TheDesigner

Reputation: 61

This will Helpful to you

**SEE DEMO http://jsfiddle.net/dhaval17/W3Jsy/**

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388446

Use .nextUntil()

$('.primary').click(function(){
    $(this).nextUntil('.primary').text($(this).text())
});

Demo: Fiddle

Upvotes: 1

Related Questions