You can't insert 'pieces' of HTML, you need the complete elements, as when they are inserted the browser automatically "fixes" your markup and adds the missing closing/opening tags. To avoid that you must concatenate everything into a string, and then append the whole string at once - that's also faster.
But an easier way to achieve this is looping over the H3 elements, add the <p> next to it and then wrap both: $('h3').each(function(i){ $(this).add( $(this).next('p') ).wrapAll('<div class="t'+i+'"/>'); }); - ricardo On Dec 23, 8:11 am, "j.schmid...@googlemail.com" <j.schmid...@googlemail.com> wrote: > hi guys ... > > my html is something like that > > <h3>head1</h3> > <p>content content</p> > <h3>head2</h3> > content content content > > Now i want to add divs to split this into containers. It should look > like that > > <div class="1"> > <h3>head1</h3> > <p>content content</p> > </div> > <div class="2"> > <h3>head2</h3> > content content content > </div> > > What is the best way to do this?? > I started with somethink like that: > > var first = jQuery('div.prod_left_content h3:first').html(); > jQuery('div.prod_left_content h3').each(function(){ > var catname = jQuery(this).html(); > if (catname==first) { > jQuery(this).before('<div class="t'+z+'">'); > } else { > jQuery(this).before('</div><div > class="t'+z+'">'); > } > z++; > }); > // last clos div tag > var lastel = jQuery('div.prod_left_content *:last'); > jQuery(lastel).after('</div>'); > > The problem with that is that before ignores the div close tag. > > Thanks for help