$(selector).replaceWith(newContent, function(index))
<!DOCTYPE html>
<html>
<head>
<title> jQuery replaceWith() method </title>
<script src= "/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
<style>
div {
border: 2px dashed blue;
width: auto;
margin: 8px;
font-size: 23px;
text-align: center;
}
h1, h2{
background-color: yellow;
color: blue;
}
#d3{
border: 2px solid blue;
width: 150px;
height: 15px;
}
</style>
</head>
<body>
<p> This is an example of using the jQuery replaceWith() method. </p>
<div id = "d1"> This is a div element </div>
<div id = "d2"> This is another div element. </div>
<p id = "p1"> Click the following button to see the effect </p>
<button id = "btn"> Click me </button>
<script>
$(document).ready(function(){
$("button").click(function() {
$("#d1").replaceWith("<h1> Hello world :) </h1>");
$("#d2").replaceWith("<h2> Welcome to the bianchenghao6.com </h2>");
$("#p1").replaceWith("<p> This is a paragraph element </p>");
$("#btn").replaceWith("<div id = 'd3'> </div>");
});
});
</script>
</body>
</html>
This is an example of using the jQuery replaceWith() method.
Click the following button to see the effect
<!DOCTYPE html>
<html>
<head>
<title> jQuery replaceWith() method </title>
<script src= "/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
<style>
div {
border: 2px dashed blue;
width: auto;
margin: 8px;
text-align: center;
}
h3{
background-color: yellow;
color: blue;
}
</style>
</head>
<body>
<p> This is an example of using the jQuery replaceWith() method. </p>
<div> This is a div element </div>
<p class = "para"> This is a paragraph element </p>
<div> This is another div element </div>
<p class = "para"> This is another paragraph element </p>
<p class = "para"> Click the following button to see the effect </p>
<button id = "btn"> Click me </button>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".para").replaceWith(function(index){
return "<h4> This is a paragraph element with index: " + index + ".</h4>";
});
$("div").replaceWith(function(index){
return "<h3> This is a div element with index: " + index + ".</h3>";
});
});
});
</script>
</body>
</html>