jQuery Untils: nextUntil, prevUntil, parentsUntil
jQuery Untils provides three very simple, but very useful methods: nextUntil, prevUntil, and parentsUntil. These methods are based on their nextAll, prevAll, and parents counterparts, except that they allow you to stop when a certain selector is reached. Elements are returned in “traversal order”.
As of jQuery 1.4, these methods are now included in jQuery core! See the patch as well as the official documentation on the .prevUntil, .nextUntil and .parentsUntil methods.
- Release v1.1
- Tested with jQuery 1.3.2 in Internet Explorer 6-8, Firefox 2-3.7, Safari 3-4, Chrome 4-5, Opera 9.6-10.1.
- Download Source, Minified (0.6kb)
- Follow the project on GitHub project page or report a bug!
- View Full Documentation
- View Unit Tests
- Examples: nextUntil, prevUntil, parentsUntil
Note that these methods take a less naïve approach than others bearing the same names, and are designed to actually return elements in traversal order, despite the element ordering flaws inherent in the jQuery 1.3.2 selector engine.
Basic usage
// Select all list items between the first and last. $("ul > :first").nextUntil( ":last" ); // The same elements, in the opposite order. $("ul > :last").prevUntil( ":first" ); // Select all "div" elements between .foo and .bar $(".foo").nextUntil( ".bar", "div" ); // Select all parent elements, excluding body and html. $(".foo").parentsUntil( "body" );
Advanced usage
This is not rocket science. There really is no “advanced” usage!
But, seriously
This plugin is going to do exactly what you expect, except that it takes care of some general jQuery 1.3.2 selector issues when there are multiple elements in the initial selector. This might be an edge-case, but it exists nonetheless.
Just take a look at the nextUntil, prevUntil and parentsUntil examples to see what’s going on.
If you have any non-bug-related feedback or suggestions, please let me know below in the comments, and if you have any bug reports, please report them in the issues tracker, thanks!
a
,b
,i
,br
,p
,strong
,em
,pre
,code
.<pre class="brush:js"></pre>
(supported syntax highlighting brushes:
js
,css
,php
,plain
,bash
,ruby
,html
,xml
)<
instead of<
and>
instead of>
in the examples themselves.