jQuery replaceWith() with Examples

The replaceWith() method in jQuery is used to replace selected elements with new content.


The syntax is as follows −


Above, the content parameter is the content to insert, whereas function is to return the content to replace.


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

 Live Demo

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


This will produce the following output −

Now, click the button to update headings −