jQuery replaceAll() Method
jQuery replaceAll() method replaces all selected elements with the new HTML elements.
jQuery replaceAll() method is similar to replaceWith() method, but there is one difference is in the syntax, placement of the content and target element.
Syntax
Here is a syntax for the replaceAll() method
$(content).replaceAll(selector);
Parameter | Type | Description |
---|---|---|
content | HTMLString | Required Specifies the HTML content to insert. |
selector | Selector | Required Specifies the selector expression, which elements to be replaced. |
Example
Here, in this example replaceAll() method call on selected elements when click on button.
<!DOCTYPE html>
<html>
<head>
<title>jQuery replaceAll() method</title>
<script src="jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("<p>Paragraph</p>").replaceAll(".param");
});
});
</script>
</head>
<body>
<div>
<p class="param first">First paragraph start here...</p>
<p class="param second">Second paragraph start here...</p>
<p class="param third">Third paragraph start here...</p>
</div>
<button>Click here to call replaceAll() method</button>
</body>
</html>