Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery's .wrap on all children of a given tag

Tags:

jquery

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?

like image 983
T. Stone Avatar asked Nov 19 '09 02:11

T. Stone


2 Answers

$('sometag').children().wrapAll("<div></div>");
like image 131
Blake Taylor Avatar answered Nov 07 '22 01:11

Blake Taylor


$('tag').wrapInner('<div>');
like image 39
Sindre Sorhus Avatar answered Nov 07 '22 02:11

Sindre Sorhus