How to Replace an Element using JQuery

You have an element on the page which you want to replace with a new element. You can use JQuery replaceWith method as shown below:

This is the element you want to replace:

<span id="message">Message</span>

The following JQuery code will replace the above span element with new div element

$("#message").replaceWith("<div id='message'>Replaced Message</div>");



About author

User avatar

Waqas Anwar

Founder of, Senior Consultant, Microsoft .NET and Web Technologies