jQuery wrapping text and elements between <hr> tags -


I need to wrap all the text, which includes free text, which is two & lt; Hr & gt; is between elements.

Looking at this source:

Advertise on this website & lt; A href = Maurice html & gt; Maurice & lt; / A & gt; This type of director, and Fusibbus is now & lt; Div & gt; & Lt; Img src = foo.png / & gt; & Lt; / Div & gt; & Lt; Hr grade = and & gt;

I need to wrap everything between hr.begin and hr.end tags, such as:

  & lt; Hour class = start> & Lt; Div class = content & gt; Advertise on this website & lt; A href = Maurice html & gt; Maurice & lt; / A & gt; This type of director, and Fusibbus is now & lt; Div & gt; & Lt; Img src = foo.png / & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Hr grade = and & gt; I can not use a method like  .nextUntil ('hr.end')  because it will not select untagged text. 

updated

Better than the previous version:

(partially answered by JP .)

  $ ( 'Hr.begin'). Each (function () {var $ set = $ (); var nxt = this.next sbing; while (nxt) {if (! $ (Nxt) .is ('hr.end')) {$ set.push ( Nxt); nxt = nxt .nextSibling;} and break;} $ set.wrapAll ('& lt; div class = "content" />);});  

Original Answer

Try it out:

If there is more than one set to wrap , Then there will be some adjustments required.

  var foundBegin = false; Var foundEnd = false; $ ('Hr.begin'). Parents () .contents () .filter (function () {if ($ (this) .is ('hr.begin')) {foundBegin = true;} if ($ (this) .is ('hr.end ')) {FoundEnd = true;} returns were found and amp;; amp;; foundEnd;}). WrapAll ('& lt; div class = "content" />');  

Returns all the nodes including the jQuery .contents () text nodes. So here we go across .corp () to hr.begin , get all your nodes using .contents () Filter them again, when we get the beginning and the end, and tracking only when returning the elements between them.

Then we can send them

  • by using .wrapAll () EDIT:
  • Edit: If multiple sets are for wrapping Try it out:

    Edit: Clear something in both examples.


    Comments