You can do it like this:
$('#id').nextUntil('#id2').andSelf().add('#id2')
It’s important to note that .nextUntil()
does not include the element it’s going until, it stops with the one before it. To add that element, you need to call .add(selector)
on the end.
You also need a .andSelf()
to include the first element
Update August 2017
jQuery method .andSelf()
is now deprecated in jQuery 1.8+, use .addBack()
instead to add the first selection back. Also,.prevUntil()
can be used if last element (#id2) percedes the first element (#id1) in the selection. Compare.index()
for both elements to determine their order in the document.