Efficient, concise way to find next matching sibling?

You can pass a multiple selector to .nextAll() and use .first() on the result, like this:

var nextFoo = $(this).nextAll("div.foo, div.something, div.else").first();

Edit: Just for comparison, here it is added to the test suite: http://jsperf.com/jquery-next-loop-vs-nextall-first/2 This approach is so much faster because it’s a simple combination of handing the .nextAll() selector off to native code when possible (every current browser) and just taking the first of the result set….way faster than any looping you can do purely in JavaScript.

Leave a Comment