jQuery replaceAll() with Examples

The replaceAll() method in jQuery is used to replace selected elements with new HTML elements.


The syntax is as follows −


Above, the content parameter is the content to insert, whereas the selector specifies which elements to be replaced.


Let us now see an example to implement the jQuery replaceAll() method 

 Live Demo

<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
         $("<h2>New Heading</h2>").replaceAll("h2");
div {
   margin: 10px;
   width: 60%;
   border: 2px dashed orange;
   padding: 5px;
<h2>Demo Heading 1</h2>
<h2>Demo Heading 2</h2>
<h2>Demo Heading 3</h2>
<h2>Demo Heading 4</h2>
<p>Click the below button to update headings</p>


This will produce the following output −

Click the button to update headings −