Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Get all the next elements <a href="http://api.jquery.com/nextall/" rel="nofollow">using <code>.nextAll()</code></a>, then <a href="http://api.jquery.com/filter/" rel="nofollow">use <code>.filter()</code></a> and return the result of the condition. When the condition returns <code>true</code>, the element is kept. </p> <p>Then narrow it down to <a href="http://api.jquery.com/first/" rel="nofollow">the <code>.first()</code></a> match.</p> <pre><code>nextEl = someEl.nextAll().filter(function() { return ( someCondition ); }).first(); </code></pre> <p>Or if there will be many elements tested, and you don't want to run the condition all those extra times, use <code>.each()</code>, then <code>return false;</code> when the condition is met. This halts the loop.</p> <pre><code>var nextEl; someEl.nextAll().each(function() { if( someCondition ) { nextEl = this; // reference the matched element return false; // break the loop } }); </code></pre> <hr> <p><strong>EDIT:</strong> If you don't want to select <em>all</em> the next elements, I'd go native, and use a <code>while</code> loop, something like this:</p> <pre><code>var nextEl = someEl[0]; while( nextEl = nextEl.nextSibling ) { if( someCondition ) { break; } } </code></pre> <p>The loop will break as soon as the condition is met, and the most recent assignment to <code>nextEl</code> will be your element. </p> <p>If the condition is never met, the loop will end when it runs out of elements, and <code>nextEl</code> will be <code>null</code> or <code>undefined</code> ( I don't remember which ).</p> <p>This should be a very quick way to do it.</p> <hr> <p><strong>EDIT:</strong></p> <p>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 <code>undefined</code>.</p> <pre><code>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); }); </code></pre> <hr> <p><strong>LAST EDIT:</strong> </p> <p>I guess I might as well make it into a plugin:</p> <pre><code>(function( $ ) { $.fn.nextMatch = function( func ) { return this.map(function() { var el = this; while( el = el.nextSibling ) { if( el.nodeType === 1 &amp;&amp; func.call( el ) ) { return el; } } }); } })( jQuery ); var nextEl = someEl.nextMatch( function() { return (someTest); }); </code></pre> <p>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 <code>while</code> loop still breaks once a match is found.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload