Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3437546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:04:44+00:00 2026-05-18T08:04:44+00:00

How can I get the nearest next element that sattisfies some condition/function? Like: nextEl

  • 0

How can I get the nearest next element that sattisfies some condition/function? Like:

nextEl = someEl.next(function() {... return true to return this from next() });

I’m not aware that the .next() method in jQuery API supports a function as an argument. Is there some trick to write it like this without doing a while loop?

Also I don’t want to select all elements first and then use filter() — this suboptimal from performance point of view.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:04:44+00:00Added an answer on May 18, 2026 at 8:04 am

    Get all the next elements using .nextAll(), then use .filter() and return the result of the condition. When the condition returns true, the element is kept.

    Then narrow it down to the .first() match.

    nextEl = someEl.nextAll().filter(function() {
        return ( someCondition );
    }).first();
    

    Or if there will be many elements tested, and you don’t want to run the condition all those extra times, use .each(), then return false; when the condition is met. This halts the loop.

    var nextEl;
    
    someEl.nextAll().each(function() {
        if( someCondition ) {
            nextEl = this;  // reference the matched element
            return false;   // break the loop
        }
    });
    

    EDIT: If you don’t want to select all the next elements, I’d go native, and use a while loop, something like this:

    var nextEl = someEl[0];
    
    while( nextEl = nextEl.nextSibling ) {
        if( someCondition ) {
            break;
        }
    }
    

    The loop will break as soon as the condition is met, and the most recent assignment to nextEl will be your element.

    If the condition is never met, the loop will end when it runs out of elements, and nextEl will be null or undefined ( I don’t remember which ).

    This should be a very quick way to do it.


    EDIT:

    Here’s a function version of it. It accepts the starting element and a function that runs the test. Then it returns the match found, or undefined.

    function nextMatch( el, func ) {
        while( el = el.nextSibling ) {
            if( func() ) {
                return el;
            }
        }
    }
    
      // nextEl will contain the match, or "undefined"
    var nextEl = nextMatch( someEl, function() {
        return (someTest);
    });
    
    var someOtherEl = nextMatch( someEl, function() {
        return (someOtherTest);
    });
    

    LAST EDIT:

    I guess I might as well make it into a plugin:

    (function( $ ) {
        $.fn.nextMatch = function( func ) {
            return this.map(function() {
                var el = this;
                while( el = el.nextSibling ) {
                    if( el.nodeType === 1 && func.call( el ) ) {
                        return el;
                    }
                }
            });
        }
    })( jQuery );
    
    var nextEl = someEl.nextMatch( function() {
        return (someTest);
    });
    

    So now it’s more of a jQuery type solution. You still should have better performance since it doesn’t fetch all the next siblings, and the while loop still breaks once a match is found.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Logging can get complicated, quickly. Considering that you have some code, how do you
I can get the element like this $(#txtEmail) but I'm not sure how to
How can I find what table cell contains time that is nearest the current
I can get the view function from request.path : from django.core.urlresolvers import resolve view_func,
I know we can get some path with <?php bloginfo('something');?> into php files, but
Can we get an ivar or a property from a NSString like we can
Can we get the variables in the query string in Node.js just like we
Can I get a MAC address that are connected to my site. this code
I can get errors to be shown after the form element, but not directly
I can get easily see what projects and dlls a single project references from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.