You are here: Reference > JavaScript > client-side > HTML DOM > methods > replaceNode

replaceNode method

Browser support:
Replaces the current element with the specified element.
For a cross-browser solution, use the replaceChild method instead. See the example below.


object.replaceNode (newElement);
You can find the related objects in the Supported by objects section below.


Required. Reference to the element to replace the current element.

Return value:

Returns a reference to the removed element (current element).

Example HTML code 1:

This example illustrates the use of the replaceNode method:
    <script type="text/javascript">
        function MakeFirst (button) {
            var container = document.getElementById ("container");
            var firstButton = container.getElementsByTagName ("button")[0];
            if (button != firstButton) {
                if (firstButton.replaceNode) {
                    firstButton.replaceNode (button);
                else {
                    container.replaceChild (button, firstButton);
                container.insertBefore (firstButton, button.nextSibling);
    <div id="container">
        <button onclick="MakeFirst (this)" style="color:red">Make me the first</button>
        <button onclick="MakeFirst (this)" style="color:green">Make me the first</button>
        <button onclick="MakeFirst (this)" style="color:blue">Make me the first</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content