Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

replaceChild method

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)

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: IHTMLDOMNode

Returns a reference to the object that is replaced.

Standards information

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.

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>

See also

a
abbr
acronym
address
attribute
b
bdo
big
blockQuote
body
button
caption
center
cite
code
col
colGroup
comment
dd
del
dfn
dir
div
dl
documentType
dt
em
fieldSet
font
form
frameSet
head
hn
html
i
iframe
input type=button
input type=checkbox
input type=email
input type=file
input type=hidden
input type=image
input type=number
input type=password
input type=radio
input type=range
input type=reset
input type=search
input type=submit
input type=tel
input type=text
input type=url
ins
kbd
label
legend
li
listing
map
marquee
menu
nextID
ol
option
p
plainText
pre
ProcessingInstruction
q
s
samp
select
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
xmp

 

 

Show:
© 2014 Microsoft