The Wayback Machine - https://web.archive.org/web/20240703105811/https://www.geeksforgeeks.org/how-to-replace-html-element-in-jquery/
Open In App

How to replace HTML element in jQuery ?

Last Updated : 22 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

We can replace HTML elements using the jQuery .replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.

The .replaceWith() method removes content from the DOM and inserts new content in its place with a single call.

Syntax :

replaceWith( newContent )
.replaceWith( function )

Return Value: This method returns the selected element with the change.

Note: The jQuery .replaceWith() method, returns the jQuery object so that the other methods can be chained onto it. However, it must be noted that the original jQuery object is returned. This object refers to the element that has been removed from the DOM, not the new element that has replaced it.

Example 1: We fetch the element we need to replace and write a new element in place of it.

HTML




<!DOCTYPE html>
<html>
    <head>
        <script
            src=
        </script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $("p").replaceWith
                    ("<div style='width:200px;height:100px;\
                    background-color:red;text-align:center;\
                    vertical-align:middle;display:table-cell'>\
                    <strong>new div</strong></div>");
                });
            });
        </script>
    </head>
    <body> 
  
        
<p>Example paragraph.</p>
  
      <button style="margin: 20px 0px;">
        click to replace 
      </button>
    </body>
</html>


Output :

Example 2: We can also replace an HTML element with another existing HTML element.

XML




<!DOCTYPE html>
<html>
    <head>
        <script src=
        </script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $("p").replaceWith($("h1"))
                });
            });
        </script>
    </head>
    <body> 
  
        
<p>Example paragraph.</p>
  
      <button style="margin: 20px 0px;">
        click to replace 
      </button>
  
      <h1>H1 tag</h1>
    </body>
</html>


Output :

Example 3: We can replace multiple HTML elements at the same time.

HTML




<!DOCTYPE html>
<html>
    <head>
        <script 
            src=
        </script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $(".X").replaceWith("<h3>new element</h3>")
                });
            });
        </script>
    </head>
    <body> 
  
      <p class="X">Example paragraph.</p>
  
      <h1 class="X">Example H1</h1>
      <div 
         style="width: fit-content;
                background-color: green;
                padding: 10px;" class="X">
         Example div
      </div>
      <button style="margin: 20px 0px;">
         click to replace 
      </button>
    </body>
</html>


Output :



Similar Reads

How to use jQuery to Search and Replace HTML elements?
Using jQuery, you can dynamically search and replace HTML elements on a webpage. By selecting elements and binding actions to events, you can manipulate their content or structure in response to user interactions or other triggers ApproachIntegrate the jQuery library hosted on a content delivery network (CDN), ensuring access to jQuery functionalit
2 min read
Replace a DOM element with another DOM element in place
The task is to replace an element by another element in place using JavaScript. Below are the few methods: parentNode Property: This property returns the parent node of the defined node, as a Node object. Syntax: node.parentNode Return value: It returns a node object, which represents the parent node of a node, or null if there is no parent. replac
3 min read
How to replace an HTML element with another one using JavaScript?
Replacing an HTML element with another using JavaScript allows for dynamic modification of webpage content without reloading the entire page. Document Object Model (DOM) is a platform and language-neutral interface that is used by programs and scripts to dynamically access the content, style, and structure. Below are the approaches to replacing an
2 min read
How to replace innerHTML of a div using jQuery?
For replacing innerHTML of a div with jquery, html() function is used. Syntax: $(selector).html() Example: &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;title&gt; changing-innerhtml-using-jquery &lt;/title&gt; &lt;link href= &quot;https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css
1 min read
Replace live() with on() in jQuery
The following jQuery event handler functions are basically used for attaching events to elements or selectors but are very different from each other. Before turning jQuery live() into on() function, let us first learn about the event handlers as well as their differences. jQuery live() : This jQuery function attaches a function handler for all the
8 min read
How to replace text after a nested tag in jQuery ?
In this article, we will see how to replace text after a nested tag using jQuery. There is one main approach that can be followed. Approach: We use the click(), contents() and filter() methods and this, nodeType and nodeValue properties. There is one division element with an id of outer-tag with a span tag with a class of nested-tag nested within i
2 min read
How to replace jQuery with Vue.js ?
In this article, we will see how to replace jQuery with Vue. jQuery: jQuery is a fast and rich JavaScript library. It is an application programming interface (API), a cross-platform JavaScript library designed to improve web browser features. It makes things easier like works across HTML document traversal and manipulation, event handling, etc. Vue
6 min read
jQuery | Move an element into another element
There are two methods to move an element inside another element are listed below: Method 1: Using append() method: This append() Method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html) ) Parameters: This method accepts two parameters as mentioned above and descr
2 min read
How to remove parent element except its child element using jQuery ?
Given an HTML document and the task is to remove the parent element except for its child element. Approach 1: Use contents() method to select all the direct children, including text and comment nodes for the selected element.Selected elements are stored in a variable.Now, use replaceWith() method to replace the content of parent element by its all
2 min read
How to select direct parent element of an element in jQuery ?
In this article, we will select the direct parent element of an element using jQuery. To select the direct parent element, the parent() method is used. This method finds the parent element related to the selected element. This parent() method traverses a single level up the selected element and returns that element. Syntax: $(selector).parent() Exa
1 min read
three90RightbarBannerImg