jQuery Document Operation - replaceAll() Method

Example

Replace each paragraph with bold text:

$(".btn1").click(function(){
   $("p").replaceAll("<b>Hello world!</b>");
});

Try it yourself

Definition and usage

The replaceAll() method replaces the selected element with specified HTML content or an element.

Tip:replaceAll() and replaceWith() The effects are the same. The difference lies in the syntax: the position of content and selector, as well as the ability of replaceWith() to use functions for replacement.

Syntax

content).replaceAll(selector)
Parameters Description
content

Required. Specifies the content to be replaced by the selected element.

Possible values:

  • HTML code - for example ("<div></div>")
  • New element - for example (document.createElement("div"))
  • Existing elements - for example ($(".div1"))

Existing elements will not be moved, but will be copied and wrapped around the selected element.

selector Required. Specifies the element to be replaced.

More examples

Use the new element to replace the element
Use document.createElement() to create a new DOM element, and then use it to replace the selected element.