⊗jqDmCWr 48 of 113 menu

Creating and Wrapping Elements in jQuery

Before wrapping the tag, you can first create the element in the document. Let's wrap the paragraphs below in '<div></div>' tags:

<p class="www">text</p> <p class="www">text</p> <p class="www">text</p> <p>text</p>

To do this, you can first create an element in the document using document.createElement('div'):

$('.www').wrap(document.createElement('div'));

Or:

let div = document.createElement("div"); $('.www').wrap(div);

You can pass not only the tag name as a parameter, but also this construction - '<div></div>' - in this case the effect will be absolutely the same:

$('.www').wrap('<div></div>');

HTML the code will look like this:

<div><p class="www">text</p></div> <div><p class="www">text</p></div> <div><p class="www">text</p></div> <p>text</p>

When using the second method, you can write any attributes in the opening tag (in our case '<div>'), and the wrapping will be done along with these attributes.

Let's wrap our paragraphs in a div with a class of zzz:

$('.www').wrap('<div class="zzz"></div>');

HTML the code will look like this:

<div class="zzz"><p class="www">text</p></div> <div class="zzz"><p class="www">text</p></div> <div class="zzz"><p class="www">text</p></div> <p>text</p>

Wrap each h3 in div with class aaa.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline