English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery replaceWith() Method

jQuery HTML/CSS Methods

The replaceWith() method replaces the selected element with new content.

The replaceWith() method is similar toreplaceAll()butcontentandselectorOn the contrary.

Syntax:

Replacement content:

$(selector).replaceWith(content)

Use feature to replace content:

$(selector).replaceWith(function(index))

Instance

Replace all paragraphs with <h1>Element:

$("button").click(function(){
  $("p").replaceWith("<h1>New Title</h1>");
});
Test See‹/›

Use feature to replace content:

$("button").click(function(){
  $("p").replaceWith(function(i){
    return "<h2>This element has index " + i + ".</h2>";
  });
});
Test See‹/›

Parameter Value

ParameterDescription
contentSpecify the content to be inserted (can include HTML tags)

Possible values:

  • HTML Element

  • DOM Element

  • jQuery Object

function(index)Specify a function that returns the HTML content to be replaced
  • index-Return the index position of the element in the collection

jQuery HTML/CSS Methods