Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 935011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:05:27+00:00 2026-05-15T21:05:27+00:00

I need to wrap everything, including free text, that is between two <hr> elements.

  • 0

I need to wrap everything, including free text, that is between two <hr> elements.

Given this source:

<hr class=begin>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  <a href=mauris.html>Mauris</a> id diam turpis, et faucibus nunc.
  <div><img src=foo.png /></div>
<hr class=end>

I need to wrap everything between the hr.begin and hr.end tags, like so:

<hr class=begin>
  <div class=content>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    <a href=mauris.html>Mauris</a> id diam turpis, et faucibus nunc.
    <div><img src=foo.png /></div>
  </div>
<hr class=end>

I cannot use a method like .nextUntil('hr.end') because this will not select the untagged text.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:05:28+00:00Added an answer on May 15, 2026 at 9:05 pm

    Updated

    I like this version better than my previous: http://jsfiddle.net/LbmCg/3/

    (Partly inspired from the answer J-P gave.)

    $('hr.begin').each(function(){
        var $set = $();
        var nxt = this.nextSibling;
        while(nxt) {
            if(!$(nxt).is('hr.end')) {
                $set.push(nxt);
                nxt = nxt.nextSibling;
            } else break;
        } 
       $set.wrapAll('<div class="content" />');
    });
    

    Original answer

    Try this: http://jsfiddle.net/LbmCg/

    If there’s more than one set to wrap, there will some adjustment needed.

    var foundBegin = false;
    var foundEnd = false;
    
    $('hr.begin').parent()
        .contents()
        .filter(function() {
            if($(this).is('hr.begin')) {
                foundBegin = true;
            }
            if($(this).is('hr.end')) {
                foundEnd = true;
            }
            return foundBegin && !foundEnd;
        })
    
        .wrapAll('<div class="content"/>');​
    

    jQuery’s .contents() returns all nodes including text nodes. So here we traverse to the .parent() of the hr.begin, get all of its nodes using .contents() then filter through them, tracking when we’ve found the beginning and the end, and only returning the elements between them.

    Then we use .wrapAll() to wrap them with the div.content.

    • http://api.jquery.com/contents/
    • http://api.jquery.com/filter/
    • http://api.jquery.com/is/

    EDIT: If there are multiple sets to wrap, try this: http://jsfiddle.net/LbmCg/1/

    EDIT: Cleaned things up a little in both examples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.