javascriptjqueryperformancenextuntil

Is there a better way to detect if nextUntil stopped regularly?


I wonder if there is a well performing way to find out if .nextUntil() stopped without matching the specified selector.

.nextUntil() returns the same result as .nextAll() if the selector didn't match any element.

So it is possible to find out using:

if ($("#id").nextUntil(".someclass").length === $("#id").nextAll().length) {
  //do some stuff if there was no match
}

But is there a better solution?


Solution

  • If it stops without a match, it means that you have a last child inside your object. You can use the following:

    if (!$("#id").nextUntil(".someclass").is(':last-child'))
    

    Test it here