Replaces an existing child element with a new child element.
Syntax
oReplace = object.replaceChild(oNewNode, oOldNode)
Parameters
oNewNode Required. Object that specifies the new element to be inserted into the document. oChildNode Required. Object that specifies the existing element to be replaced.
Return Value
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.
Example
This example uses the replaceChild method to replace a bold element from a DIV with an italic element.
Sample Code
<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 <B>word</B> between bold and italic.</DIV> </BODY>
Applies To
A, ACRONYM, ADDRESS, B, BDO, BIG, BLOCKQUOTE, BODY, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, DD, DEL, DFN, DIR, DIV, DL, DT, EM, FIELDSET, FONT, FORM, FRAMESET, HEAD, Hn, HTML, I, IFRAME, INPUT type=button, INPUT type=checkbox, INPUT type=file, INPUT type=hidden, INPUT type=image, INPUT type=password, INPUT type=radio, INPUT type=reset, INPUT type=submit, INPUT type=text, INS, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, NEXTID, OL, OPTION, P, PLAINTEXT, PRE, Q, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, XMP