The replaceAll() method in JQuery is used to replace HTML elements with new HTML elements.
<html> <head> <title> My First Programme </title> </head> <style> div { background-color: violet; } </style> <body> <h1> JQuery </h1> <p>New Code</p> <button> Click Here </button> <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script> <script> $('button').click( function() { $("<div>Hello World</div>").replaceAll('p'); }); </script> </body> </html>
So, in the above code, we have a <p> element,
<p>New Code</p>
And we are going to replace it with a new <div> element.
<div>Hello World</div>
And we have achieved it using the JQuery statement,
$('button').click( function() { $("<div>Hello World</div>").replaceAll('p'); });
All we are doing is, using the replaceAll() method on the <p> element, to replace the <p> element with the <div> element.
$("<div>Hello World</div>").replaceAll('p');
And we got the <p> element replaced with the <div> element violet in color.
And why is the color violet?
Because we have set the style property for <div> element.
<style> div { background-color: violet; } </style>