replaceChild method (Internet Explorer)

Switch View :
ScriptFree
replaceChild method

[This documentation is preliminary and is subject to change.]

Replaces an existing child element with a new child element.

Document Object Model (DOM) Level 3 Core Specification, Section 1.4

Syntax

object.replaceChild(newChild, oldChild)

Standards information

Parameters

newChild [in]

Type: IHTMLDOMNode

Object that specifies the new element to be inserted into the document.

oldChild [in]

Type: IHTMLDOMNode

Object that specifies the existing element to be replaced.

Return value

Type: Object

Returns a reference to the object that is replaced.

Remarks

The node to be replaced must be an immediate child of the parent object. The new node must be created using the createElement method.

This property is accessible at run time. If elements are removed at run time, before the closing tag is parsed, areas of the document might not render.

Windows Internet Explorer 9. Exceptions are only supported when webpages are displayed in IE9 Standards mode.

In Microsoft Internet Explorer 6, This method now applies to the attribute object.

Examples

This example uses the replaceChild method to replace a bold element from a div with an italic element.


<head>
<script>
function replaceElement()
{
        //The first child of the div is the bold element.   
    var oChild=Div1.children(0);	
    var sInnerHTML = oChild.innerHTML;
    if (oChild.tagName=="B")
    {
        oNewChild=document.createElement("I");
        Div1.replaceChild(oNewChild, oChild);
        oNewChild.innerHTML=sInnerHTML
    }
    else
    {
        oNewChild=document.createElement("B");
        Div1.replaceChild(oNewChild, oChild);
        oNewChild.innerHTML=sInnerHTML		
    }
}
</script>
</head>
<body>
<div id="Div1" onclick="replaceElement()">
Click anywhere in this sentence to toggle this <strong>word</strong>
between bold and italic.</div>
</body>

 

 

Build date: 2/14/2012