jQuery wrap() method

50 views Asked by At

Is it possible to wrap all three <span> in one <div> ?

<span class='wrap'>Span 1</span>

<span class='wrap'>Span 2</span>

<span class='wrap'>Span 3</span>

Here is a wrapper <div id="parentWrapper"></div>

Upon my running code below each span has an individual <div> and alerts three times its context

$(document).ready(function(){
    $('span.wrap').wrap('<div id="parentWrapper">');
    $('div#parentWrapper').each(function(){
        alert($(this).html());
    });
});
1

There are 1 answers

0
charlietfl On BEST ANSWER

There is a wrapAll() method

Description: Wrap an HTML structure around all elements in the set of matched elements.

 $('span.wrap').wrapAll('<div id="parentWrapper">');

Reference wrapAll() docs