replaceChild method
Replaces the specified child element of the current element with a new element.
The element to be replaced must be an immediate child of the current element.
The removed child is no longer present in the document tree, but with the reference returned by the replaceChild method it can be inserted later into any element in the same document.
- Use the insertBefore or the appendChild method to insert it into the same document.
- If you want to insert the removed node into another document, use the adoptNode or importNode method.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. Reference to the element to insert. | |||||||
Required. Reference to the child element of the current element to be replaced. |
Return value:
Returns a reference to the removed element.
Example HTML code 1:
This example illustrates the use of the replaceChild method:
|
||||
<head> <script type="text/javascript"> function MakeFirst (button) { var container = document.getElementById ("container"); var firstButton = container.getElementsByTagName ("button")[0]; if (button != firstButton) { container.replaceChild (button, firstButton); container.insertBefore (firstButton, button.nextSibling); } } </script> </head> <body> <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> </div> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
attribute, document, DocumentFragment, XMLDocument
HTML elements:
a, abbr, acronym, address, b, bdo, big, blink, blockquote, body, button, caption, center, cite, code, col, colgroup, dd, del, dfn, dir, div, dl, dt, em, fieldset, font, form, frameset, h1, h2, h3, h4, h5, h6, head, html, i, img, ins, kbd, keygen, label, legend, li, listing, map, marquee, menu, nobr, noframes, noscript, object, ol, optgroup, option, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, u, ul, var, xml, xmp
Related pages:
External links:
User Contributed Comments