Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I came up with in the end.</p> <pre><code>// here's our active element. var $active = $('#active'); // here is the filter we'll be testing against. var filter = "img.a"; // $all will be the final jQuery object with all the consecutively matched elements. // start it out by populating it with the current object. var $all = $active; for ($curr = $active.prev(filter); $curr.length &gt; 0; $curr = $curr.prev(filter)) { $all = $all.add($curr); } for ($curr = $td.next(filter); $curr.length &gt; 0; $curr = $curr.next(filter)) { $all = $all.add($curr); } </code></pre> <p>For a follow up question, I could see how this could easily be generalised by making it into a function which takes two arguments: an initial element, and a filter string - can anyone point me in the right direction to find out how to extend the jQuery object to add such a function?</p> <hr> <p><strong>Edit</strong>: I've since found that the each() function would do this rather well for some purposes. In my own case it doesn't work as cleanly, since I want a single jQuery object for all those elements, but here's how you could use each for a different purpose (hiding consecutive ".a" elements, in this example:)</p> <pre><code>$('#active') .nextAll() .each(hideConsecutive) .end() .prevAll() .each(hideConsecutive) ; function hideConsecutive(index, element) { var $e = $(element); if (!$e.is(".a")) { return false; // this stops the each function. } else { $e.hide('slow'); } } </code></pre> <p>--</p> <p>Edit: I've put this together into a plugin now. Take a look at <a href="http://plugins.jquery.com/project/Adjacent" rel="nofollow noreferrer">http://plugins.jquery.com/project/Adjacent</a> if you're interested.</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.
    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