removeChild method
Removes the specified child node from the current element.
The specified child node 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 removeChild method it can be inserted later into any element in the same document.
- Use the insertBefore or 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 remove. |
Return value:
Returns the removed node object.
Example HTML code 1:
This example illustrates the use of the removeChild method:
|
||||
<head> <script type="text/javascript"> var anchor = null; function RemoveAnchor () { if (anchor == null) { anchor = document.getElementById ("myAnchor"); anchor.parentNode.removeChild (anchor); } else { alert ("Insert the removed anchor first!"); } } function InsertAnchor () { if (anchor) { var container = document.getElementById ("container"); container.appendChild (anchor); anchor = null; } else { alert ("Remove the anchor first!"); } } </script> </head> <body> <div id="container"><a id="myAnchor" href="#">An anchor in the container</a></div> <br /><br /> <button onclick="RemoveAnchor ();">Remove the anchor from the container</button> <button onclick="InsertAnchor ();">Insert the removed anchor into the container</button> </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