Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Bind <a href="http://api.jquery.com/click/" rel="nofollow"><code>.click()</code></a> on the child spans, use <a href="http://api.jquery.com/index/" rel="nofollow"><code>.index()</code></a> to get its sequence index, then use <a href="http://api.jquery.com/gt-selector/" rel="nofollow"><code>:gt()</code></a> selector to find elements past it and use <a href="http://api.jquery.com/add/" rel="nofollow"><code>.add()</code></a></p> <pre><code>$('.main_container span').click(function(){ // get position of current element var index = $(this).index(); // delete this and all elements past this element $(this).parent().find('span:gt('+index+')').add(this).remove(); }); </code></pre> <p>Example: <a href="http://jsfiddle.net/cDZqk/" rel="nofollow">http://jsfiddle.net/cDZqk/</a></p> <p>You can also use <a href="http://api.jquery.com/children/" rel="nofollow"><code>.children()</code></a> if there's more nesting going on:</p> <pre><code>$('.main_container span').click(function(){ // get position of current element var index = $(this).index(); // delete this and all elements past this element $(this).parent().children(':gt('+index+')').add(this).remove(); }); </code></pre> <p>Example: <a href="http://jsfiddle.net/cDZqk/1/" rel="nofollow">http://jsfiddle.net/cDZqk/1/</a></p> <p><em>or, ya know, you can use <code>.nextAll</code> and let it take care of it for you. <strong>:curses:</strong></em></p> <p>The "one-liner" version of BYossarian's answer:</p> <pre><code>$('.main_container span').click(function(){ $(this).nextAll().add(this).remove(); }); </code></pre> <p>Example: <a href="http://jsfiddle.net/cDZqk/2/" rel="nofollow">http://jsfiddle.net/cDZqk/2/</a></p>
 

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