You can't insert invalid HTML into the DOM.

Use the nextUntil plugin by John Resig (I'm starting to think this
belongs in the core):

$.fn.nextUntil = function(expr) {
   var match = [];
   this.each(function(){
       for( var i = this.nextSibling; i; i = i.nextSibling ) {
           if ( i.nodeType != 1 ) continue;
           if ( jQuery.filter( expr, [i] ).length ) break;
           match.push( i );
       }
   });
   return this.pushStack( match, arguments );
};

then you can do this:

$('h2').each(function(){
   $(this).nextUntil('h2').wrapAll('<div class="contents" />');
});

cheers,
- ricardo

On Feb 19, 8:21 pm, Charlie Griefer <charlie.grie...@gmail.com> wrote:
> No answer yet.  I've tried playing around with using:$('h2').after('<div
> class="someclass">');
> $('h2:not(:first)').before('</div>');
>
> but that doesn't seem to be working (and is also problematic because it
> leaves an open <div> after the last h2).
>
> it seems that the after() and before() methods each want to insert a full
> element (opened and closed).  i can't tell, but i get the impression that
> jQuery is closing the <div class="someclass"> div automatically.
>
> hoping somebody else might jump in with a more elegant (and correct/working)
> solution.
>
> On Thu, Feb 19, 2009 at 3:17 PM, cchun...@gmail.com <cchun...@gmail.com>wrote:
>
>
>
>
>
> > Ok i have tried
>
> > $('.tabs h2').next().wrapAll('<div class="tabbody"></div>');
>
> > but it does not want to wrap all the paragraphs/tables/html in the one
> > div.
>
> > Any ideas?
>
> > Thanks for the help!
>
> > On Feb 19, 12:21 pm, Charlie Griefer <charlie.grie...@gmail.com>
> > wrote:
> > > actually i should clarify... not "change", but wrap the elements
> > following
> > > h2 elements with the specified HTML.
>
> > > On Thu, Feb 19, 2009 at 10:00 AM, Charlie Griefer <
> > charlie.grie...@gmail.com
>
> > > > wrote:
> > > > On Thu, Feb 19, 2009 at 9:27 AM, cchun...@gmail.com <
> > cchun...@gmail.com>wrote:
>
> > > >> I am trying to look through content in a div and if i find an H2 or
> > > >> whatever attribute the content or HTML that follows it will be
> > > >> encloded in a div with a class. I will do this for every H2 so if i
> > > >> have 3 H2's and some paragraphs after them it will surround all HTML
> > > >> after each of the H2 in a div.
>
> > > > This will change the element following all h2 elements in the doc.
>
> > > > <script>
> > > > $(function() {
> > > >  $('h2').next().wrap('<div class="someclass"></div>');
> > > > });
> > > > </script>
>
> > > > If you want it to be just within a certain div, and assuming that div
> > has
> > > > an id attribute of "foo", it would be:
>
> > > > <script>
> > > >  $(function() {
> > > > $('#foo h2').next().wrap('<div class="someclass"></div>');
> > > >  });
> > > > </script>
>
> > > > --
> > > > I have failed as much as I have succeeded. But I love my life. I love
> > my
> > > > wife. And I wish you my kind of success.
>
> > > --
> > > I have failed as much as I have succeeded. But I love my life. I love my
> > > wife. And I wish you my kind of success.- Hide quoted text -
>
> > > - Show quoted text -
>
> --
> I have failed as much as I have succeeded. But I love my life. I love my
> wife. And I wish you my kind of success.

Reply via email to