JQuery replaceWith()

 

In this example we are going to show how to use a function to replace selected elements with new content.

 
 
 
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> <style> p{font-size: 25px; color:crimson} h3{color:rgb(30, 220, 20)} button{height: 40px; width:250px; font-size: 20px} </style> <script> $(document).ready(function(){ $("button").click(function(){ $("p").replaceWith(function(n){ return "<h3>Welcome to Appwrk Solution Index " + n + ".</h3>"; }); }); }); </script> </head> <body> <h1>Appwrk IT Solution</h1> <p>APPWRK IT Solutions is a complete Web Solutions Company offering customer-oriented web design services and more importantly, deliver them effectively.</p> <p>APPWRK IT Solutions is a complete Web Solutions Company offering customer-oriented web design services and more importantly, deliver them effectively.</p> <p>APPWRK IT Solutions is a complete Web Solutions Company offering customer-oriented web design services and more importantly, deliver them effectively.</p> <p>APPWRK IT Solutions is a complete Web Solutions Company offering customer-oriented web design services and more importantly, deliver them effectively.</p> <p>APPWRK IT Solutions is a complete Web Solutions Company offering customer-oriented web design services and more importantly, deliver them effectively.</p> <button>Replace each p element</button> </body> </html>
Output