jQuery.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] ).r.length ) break; match.push( i ); } }); return this.pushStack( match, arguments ); }; (plugin by John Resig)
$('h1').each(function(){ $(this).nextUntil('h1').wrapAll('div'); }); cheers, - ricardo On Mar 24, 1:01 pm, Claes <claesatw...@gmail.com> wrote: > Hi, I am new to jQuery, and it seems the very first thing I try to do > gives me problems. However, probably I am missing something so let me > ask you if you can hint me on the best way to accomplish this. > > I want to transform a document with a flat structure like this > > <h1>Heading 1</h1> > <p>Paragraph 1.1</p> > <p>Paragraph 1.2</p> > <h1>Heading 2</h1> > <p>Paragraph 2.1</p> > <p>Paragraph 2.2</p> > > to something more hierarchical like this > > <h1>Heading 1</h1> > <div> > <p>Paragraph 1.1</p> > <p>Paragraph 1.2</p> > </div> > <h1>Heading 2</h1> > <div> > <p>Paragraph 2.1</p> > <p>Paragraph 2.2</p> > </div> > > The biggest problem for me is to detect where the limit between the > paragraphs (in the form of another heading) are. Unfortunately there > is nothing distinctive about the p elements (no ids, no classes, this > is generated code that I can not control) > > Thanks! > /Claes