jQuery HTML wrap() Method

jQuery HTML wrap() Method

by lucasprus

HTML

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Wrap a div element around each p element</button>

<h2>This is heading 2</h2>
<h2>This is heading 2</h2>
<h2>This is heading 2</h2>


<div class="blue">A blue div</div>
<div class="blue">A blue div</div>

CSS

div{background-color:yellow;}
div.blue{background-color:blue;}

JavaScript

$(document).ready(function() {

    $("button").click(function() {
        $("p").wrap("<div></div>");
        $("h2").wrap($("div.blue"));
    });

});