Navneet Saini
Navneet Saini

Reputation: 944

jQuery: How to use .nextUntil()?

Suppose I have HTML like this:

<span class="buttonNo1">1</span>
<span class="buttonNo2">2</span>
<span class="buttonNo3 clicked">3</span>  //Suppose this button is clicked
<span class="buttonNo4">4</span>
<span class="buttonNo5">5</span>
<span class="buttonNo6">6</span>
<span class="buttonNo7">7</span>

Now, I want to get the className of next two adjacent siblings of the span which has the class clicked when clicked is clicked.

One simple method would be.

$(".clicked").click(function(e){
  console.log($(this).next().attr('class'))
  console.log($(this).next().next().attr('class'))
})

How to do it with .nextUntil()?

Upvotes: 1

Views: 304

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Try using .nextAll() and :lt() filters

$(".clicked").click(function(e){
    $(this).nextAll(':lt(2)').css('background-color', 'red');
})

Demo: Fiddle

Upvotes: 3

Related Questions