Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this. It will mark your Element, create a set of Elements matching your selector and collect all Elements from the set following your element. </p> <pre><code>$.fn.findNext = function ( selector ) { var set = $( [] ), found = false; $( this ).attr( "findNext" , "true" ); $( selector ).each( function( i , element ) { element = $( element ); if ( found == true ) set = set.add( element ) if ( element.attr("findNext") == "true" ) found = true; }) $( this ).removeAttr( "findNext" ) return set } </code></pre> <p><em>EDIT</em></p> <p>much simpler solution using jquerys index method. the element you call the method from needs to be selectable by the same selector though</p> <pre><code>$.fn.findNext = function( selector ){ var set = $( selector ); return set.eq( set.index( this, ) + 1 ) } </code></pre> <p>to free the function from this handicap, we could youse the browsers own <a href="http://www.w3schools.com/jsref/met_node_comparedocumentposition.asp" rel="nofollow noreferrer">compareDocumentposition</a></p> <pre><code>$.fn.findNext = function ( selector ) { // if the stack is empty, return the first found element if ( this.length &lt; 1 ) return $(s).first(); var found, that = this.get(0); $( selector ) .each( function () { var pos = that.compareDocumentPosition( this ); if ( pos === 4 || pos === 12 || pos === 20 ){ // pos === 2 || 10 || 18 for previous elements found = element; return false; } }) // using pushStack, one can now go back to the previous elements like this // $("#someid").findNext("div").remove().end().attr("id") // will now return "someid" return this.pushStack( [ found ] ); }, </code></pre> <p><em>EDIT 2</em> this is far easier using jQuery's $.grep. here's the new code</p> <pre><code> $.fn.findNextAll = function( selector ){ var that = this[ 0 ], selection = $( selector ).get(); return this.pushStack( // if there are no elements in the original selection return everything !that &amp;&amp; selection || $.grep( selection, function( n ){ return [4,12,20].indexOf( that.compareDocumentPosition( n ) ) &gt; -1 // if you are looking for previous elements it should be [2,10,18] }) ); } $.fn.findNext = function( selector ){ return this.pushStack( this.findNextAll( selector ).first() ); } </code></pre> <p>when compressing variable names this becomes a mere two liner.</p> <p><em>Edit 3</em> using bitwise operations, this function may be even faster?</p> <pre><code>$.fn.findNextAll = function( selector ){ var that = this[ 0 ], selection = $( selector ).get(); return this.pushStack( !that &amp;&amp; selection || $.grep( selection, function(n){ return that.compareDocumentPosition(n) &amp; (1&lt;&lt;2); // if you are looking for previous elements it should be &amp; (1&lt;&lt;1); }) ); } $.fn.findNext = function( selector ){ return this.pushStack( this.findNextAll( selector ).first() ); } </code></pre>
    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. 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