tags:

views:

271

answers:

3

Working jQuery, what I'd like to perform is something similar to the following:

$('sometag').children().wrap('<div></div>');

With the resulting DOM manipulation wrapping all children in one div, not each child individually.

Starting example HTML:

<div>
   <h3>Heading</h3>
   <p>Paragraph</p>
</div>

What this line of code does:

<div>
    <div>
        <h3>Heading</h3>
    </div>
    <div>
        <p>Paragraph</p>
    </div>
</div>

What I want:

<div>
    <div>
        <h3>Heading</h3>
        <p>Paragraph</p>
    </div>
</div>

What is the proper syntax to achieve the end result I'm looking for?

A: 
$('sometag').html('<div>' + $('sometag').html() + '</div>');
beggs
+2  A: 
$('sometag').children().wrapAll("<div></div>");
Blake Taylor
This would not produce the output he wants. It would simply wrap the `p` tags and leave the `h3` element outside the `div`
Doug Neiner
Looks like you fixed it :)
Doug Neiner
'sometag' wouldn't work either with his sample code. I was giving a hypothetical use of wrap all. I figured the application was quite obvious.
Blake Taylor
This only works when `'sometag'` matches a single element. If not, It will move all children elements (i.e. all elements inside all `<sometag>` s) into the new `<div>`. `wrapInner` was written specifically to handle the OPs use-case, and works when matching multiple [parent] elements.
Crescent Fresh
In THIS instance, what is the difference from $('sometag').wrap('<div></div>'); and $('sometag').children().wrapAll('<div></div>'); ??
Mark Schultheiss
A: 
$('tag').wrapInner('<div />');
mofle