24 of 119 menu

The replaceWith method

The replaceWith method replaces one element with another. It works similarly to the replaceAll method.

Syntax

The method parameter can be text (possibly with tags), an array, a DOM element, or a jQuery object:

$(What that what what which as how how_replace).replaceWith(than)_replace);

The selected elements will be replaced with the content specified by the text returned by the custom function. The function is called separately for each of the selected elements:

$(selector).replaceWith(function());

Example

Let's replace all paragraphs with class www with a div with text '!!!':

<p class="www">text</p> <p class="www">text</p> <p class="www">text</p> <p>text</p> $('p.www').replaceWith('<div>!!!</div>');

HTML the code will look like this:

<div>!!!</div> <div>!!!</div> <div>!!!</div> <p>text</p>

See also

  • method replaceAll,
    which also replaces some elements with others
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