Sergio
Sergio

Reputation: 28837

Get all next siblings of element

What is the best way to get all next siblings of a element? Not just next one and not siblings before it?

This was the way I found, but is there a jQuery selector for it?

$('.someclass').click(function () {
    var chosenOne = $(this);
    $('.someclass').removeClass('extraclass');
    chosenOne.parent().find('.someclass').each(function () {
        var el = $(this).index();
        if (el >= chosenOne.index()) {
            $(this).addClass('extraclass');
        }
    });
});

FIDDLE

Upvotes: 0

Views: 172

Answers (1)

Kevin B
Kevin B

Reputation: 95022

the .nextAll() method was made for this:

chosenone.nextAll(".someclass").addBack().addClass("extraclass")

FIDDLE

Upvotes: 4

Related Questions