How to select all content between two tags in jQuery

JqueryCss SelectorsTraversal

Jquery Problem Overview


I have a document with headings and unordered lists.

How can I use JQuery to select a given heading (by its unique class name) AND all content between that heading and the next heading?

Update:

Your suggestions are great, but aren't what I'm looking for. In the below code, for example, I would like to access only the "h1" with id of "heading2" and everything up to, but not including the "h1" with id of "heading3".

The jQuery examples provided above will access everyting after the first "h" tag that is not an "h" tag.

... or, correct me if I'm wrong :)

    <h1 id="heading1">...</h1>
        <ul>...</ul>
        <p>...</p>
        <ul>...</ul>
        <p>...</p>
    <h1 id="heading2" >...</h1>
        <ul>...</ul>
        <p>...</p>
        <ul>...</ul>
        <p>...</p>
    <h1 id="heading3" >...</h1>
        <ul>...</ul>
        <p>...</p>
        <ul>...</ul>
        <p>...</p>

Jquery Solutions


Solution 1 - Jquery

Two methods in particular would be very useful solving this problem: .nextUntil, and .andSelf. The first will grab all of the siblings following your selector, and the latter will lump in whatever is matched by your selector as well, giving you one jQuery object that includes them all:

$("#heading2")
    .nextUntil("#heading3").andSelf()
        .css("background", "red");

This results in the following:

enter image description here

Solution 2 - Jquery

Here is the solution I use for my projects:

jQuery selector

(function ($) {
    $.fn.between = function (elm0, elm1) {
        var index0 = $(this).index(elm0);
        var index1 = $(this).index(elm1);

        if (index0 <= index1)
            return this.slice(index0, index1 + 1);
        else
            return this.slice(index1, index0 + 1);
    }
})(jQuery);

Usage

$('body').between($('#someid'), $('#someid2')).each(function () {
    // Do what you want.
});

Solution 3 - Jquery

$('#secondSelector').prevAll('#firstSelector ~ *')
   

Solution 4 - Jquery

Yeah, you're right. This will only avoid the desired selector. Maybe it needs to be more detailed:

$(firstSelector).nextAll().not(secondSelector).not($(secondSelector).nextAll()).text()

Solution 5 - Jquery

I feel as if the accepted answer needs a bit of an update since the release of jQuery 1.8 and later. .andSelf() has been deprecated. In its place, we have .addBack(). The documentation explains everything you need to know.

Solution 6 - Jquery

Maybe, with

$(selectorForFirstHeading).nextAll().not(selectorForLastHeading).text()

Why do I use text()? Because html() will only return the inner HTML of the first result element.

Solution 7 - Jquery

If your elements are at the same level, something like this:

<h1 id="heading1">...</h1>
<ul>...</ul>
<p>...</p>
<ul>...</ul>
<p>...</p>
<h1 id="heading2" >...</h1>

That is, your next heading element is not a child of an element at the same level as the first heading element. You can try this code:

// This will get all the following siblings of <h1 id="heading1"> except those that are headings themselves
var elements = $('#heading1').nextAll().not("h1");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionedtView Question on Stackoverflow
Solution 1 - JquerySampsonView Answer on Stackoverflow
Solution 2 - JqueryLasse EspeholtView Answer on Stackoverflow
Solution 3 - JqueryjesperView Answer on Stackoverflow
Solution 4 - JqueryRicardo VegaView Answer on Stackoverflow
Solution 5 - JqueryThomasView Answer on Stackoverflow
Solution 6 - JqueryRicardo VegaView Answer on Stackoverflow
Solution 7 - Jquerydisc0dancerView Answer on Stackoverflow