jQuery Tutorial

jQuery Methods for Replacing Elements

jQuery replaceAll() Method

The replaceAll() method in jQuery swaps out selected HTML elements for new ones.

Syntax of replaceAll method in jQuery

$(content).replaceAll(selector)

Example of jQuery replaceAll method

All

elements should be replaced with

elements:

$("button").click(function(){
$("
Hello world!
").replaceAll("p");
});

jQuery replaceWith() Method

This method replaces selected elements with new content using the replaceWith() method.

Syntax of replaceWith method in jQuery

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

Example of jQuery replaceWith method

Replace the text in the first

element with the following:

$("button").click(function(){
$("p:first").replaceWith("Hello world!");
});
Did you find this article helpful?