The Wayback Machine - https://web.archive.org/web/20250321042920/https://www.geeksforgeeks.org/jquery-replacewith-method/
Open In App

jQuery replaceWith() Method

Last Updated : 24 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The jQuery replaceWith() method replaces the selected HTML element(s) with new content or elements. It removes the existing element from the DOM and inserts the specified content in its place, allowing dynamic updates to webpage structure and content.

Syntax:

$(selector).replaceWith(content, function)

Parameters:

This method accepts two parameters as mentioned above and described below:

  • content: It is required parameter which is used to specify the content need to replace.
  • para2: It is an optional parameter which perform after calling.

Return Value: This method returns the selected element with the change. Below codes illustrate the replaceWith() method in jQuery:

Example 1: In this example we use jQuery’s replaceWith() method to replace each button with a <div> containing the button’s text when clicked. The new <div> inherits the button’s content dynamically.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>The replaceWith method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <style>
        button {
            display: block;
            margin: 10px;
            color: red;
            width: 200px;
            padding: 3px;
        }

        div {
            color: green;
            border: 2px solid green;
            width: 200px;
            margin: 3px;
            padding: 5px;
            font-size: 20px;
            text-align: center;
        }
    </style>
</head>

<body>
    <!-- click on individual button and see the change -->
    <button>Geeks</button>
    <button>for</button>
    <button>Geeks</button>

    <!-- jQuery code to show the working of this method -->
    <script>
        $("button").click(function () {
            $(this).replaceWith("<div>" + $(this).text() + "</div>");
        });
    </script>
</body>

</html>

Output:

file

jQuery replaceWith() Method Example Output

Example 2: In this example jQuery’s replaceWith() method to replace a paragraph’s content with the string “GeeksforGeeks!” when the button is clicked. An alert prompts before the content replacement.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>The replaceWith method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
    <style>
        button {
            display: block;
            margin: 4px;
            color: green;
            width: 150px;
            padding: 5px;
        }

        div {
            width: 200px;
            height: 100px;
            padding: 20px;
            border: 2px solid green;
        }
    </style>
</head>

<body>
    <div>
        <p>Welcome to </p>

        <!-- click on this button and see the change -->
        <button>Click Here!</button>
    </div>

    <!-- jQuery code to show the working of this method -->
    <script>
        let x = "GeeksforGeeks!";
        $("button").click(function () {
            $("p").replaceWith(x).replaceWith(function (n) {
                alert("Click ok and string will replace");
                return n;
            });
        });
    </script>
</body>

</html>

Output:

replaceWith

jQuery replaceWith() Method Example Output

jQuery replaceWith() Method – FAQs

Can replaceWith() be used to replace multiple elements?

Yes, replaceWith() can replace multiple elements if they are selected using a class, tag, or other selectors.

Does replaceWith() remove the original element from the DOM?

Yes, replaceWith() completely removes the original element from the DOM and inserts the new content.

Can I use replaceWith() to insert HTML or plain text?

Yes, you can pass either HTML markup or plain text to replaceWith() to replace the selected element.

How is replaceWith() different from .html() in jQuery?

replaceWith() removes and replaces the entire element, while .html() only changes the inner content of the element, keeping the element intact.

Can replaceWith() handle jQuery objects for replacement?

Yes, you can pass a jQuery object to replaceWith() to replace the selected element with another existing jQuery object.



Next Article

Similar Reads

three90RightbarBannerImg