<p>Given this example:</p>

<pre><code>&lt;img class="a" /&gt;
&lt;img /&gt;
&lt;img class="a" /&gt;
&lt;img class="a" id="active" /&gt;
&lt;img class="a" /&gt;
&lt;img class="a" /&gt;
&lt;img /&gt;
&lt;img class="a" /&gt;
</code></pre>

<p><em>(I've just used img tags as an example, that's not what it is in my code)</em></p>

<p>Using jQuery, how would you select the img tags with class "a" that are adjacent to #active (the middle four, in this example)?</p>

<p>You could do it fairly easily by looping over all the following and preceding elements, stopping when the filter condition fails, but I was wondering if jQuery could it natively?</p>

[{'answer_id': 79471, 'author': 'Xenph Yan', 'author_id': 264, 'author_profile': 'https://Stackoverflow.com/users/264', 'pm_score': 2, 'selected': False, 'text': '<p>I believe looping is your best bet. But you could try, each active, and then move before and after until the condition breaks, which if the set is large enough would be faster.</p>\n'}, {'answer_id': 79767, 'author': 'Prestaul', 'author_id': 5628, 'author_profile': 'https://Stackoverflow.com/users/5628', 'pm_score': 0, 'selected': False, 'text': '<p>The tilde (~) is the <a href="http://docs.jquery.com/Selectors/siblings#prevsiblings" rel="nofollow noreferrer">siblings selector</a>:</p>\n\n<pre><code>$(\'#active ~ img.a\').hide();\n</code></pre>\n'}, {'answer_id': 79861, 'author': 'nickf', 'author_id': 9021, 'author_profile': 'https://Stackoverflow.com/users/9021', 'pm_score': 0, 'selected': False, 'text': '<p>@Prestaul</p>\n\n<pre><code>$(\'#active ~ img.a\')\n</code></pre>\n\n<p>would only select the <em>following</em> siblings, and would include the non-consecutive siblings too. Docs: <a href="http://docs.jquery.com/Selectors/siblings#prevsiblings" rel="nofollow noreferrer">http://docs.jquery.com/Selectors/siblings#prevsiblings</a></p>\n'}, {'answer_id': 79978, 'author': 'Andrew Hedges', 'author_id': 11577, 'author_profile': 'https://Stackoverflow.com/users/11577', 'pm_score': 0, 'selected': False, 'text': "<p>This is another way to do it, though the sibling selector answer is pretty cool:</p>\n\n<pre><code>var next = $('#active').next('.a');\nvar prev = $('#active').prev('.a');\n</code></pre>\n\n<p><strong>Edit:</strong> I re-read your requirements and this isn't quite what you want. You could use nextAll and prevAll, but those, too, would not stop at the IMGs without the class name.</p>\n"}, {'answer_id': 80302, 'author': 'nickf', 'author_id': 9021, 'author_profile': 'https://Stackoverflow.com/users/9021', 'pm_score': 3, 'selected': True, 'text': '<p>Here\'s what I came up with in the end.</p>\n\n<pre><code>// here\'s our active element.\nvar $active = $(\'#active\');\n\n// here is the filter we\'ll be testing against.\nvar filter = "img.a";\n\n// $all will be the final jQuery object with all the consecutively matched elements.\n// start it out by populating it with the current object.\nvar $all = $active;\n\nfor ($curr = $active.prev(filter); $curr.length &gt; 0; $curr = $curr.prev(filter)) {\n    $all = $all.add($curr);\n}\nfor ($curr = $td.next(filter); $curr.length &gt; 0; $curr = $curr.next(filter)) {\n    $all = $all.add($curr);\n}\n</code></pre>\n\n<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>\n\n<hr>\n\n<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>\n\n<pre><code>$(\'#active\')\n    .nextAll()\n    .each(hideConsecutive)\n    .end()\n    .prevAll()\n    .each(hideConsecutive)\n;\nfunction hideConsecutive(index, element) {\n    var $e = $(element);\n    if (!$e.is(".a")) {\n        return false;    // this stops the each function.\n    } else {\n        $e.hide(\'slow\');\n    }\n}\n</code></pre>\n\n<p>--</p>\n\n<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>\n'}, {'answer_id': 763488, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>The below code will add two new functions, nextConsecutive() and prevConsecutive(). They should do what you want.</p>\n\n<p>$.each( ['prev', 'next'], function(unusedIndex, name) {\n    $.fn[ name + 'Consecutive' ] = function(matchExpr) {</p>\n\n<pre><code>    var $all = \n        (name == 'prev')\n             ? $(this).prevAll()\n             : $(this).nextAll();\n    if (!matchExpr)\n        return $all;\n\n    var $notMatch = $($all).not(matchExpr).filter(':first');\n    if ($all.index($notMatch) != -1)\n        return $allConsecutive = $all.slice(0, $all.index($notMatch));\n\n    return $all;\n};\n</code></pre>\n\n<p>});</p>\n"}]
['https://Stackoverflow.com/questions/79455', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9021/']