The removeChild() method removes a specified node.
The removeAttribute() method removes a specified attribute.
The examples below use the XML file books.xml.
A function, loadXMLDoc(), in an external JavaScript is used to load the XML file.
Remove an element node
This example uses removeChild() to remove the first <book> element.
Remove the current element node
This example uses parentNode and removeChild() to remove the current <book> element.
Remove a text node
This example uses removeChild() to remove the text node from the first <title> element.
Clear the text of a text node
This example uses the nodeValue() property to clear the text node of the first <title> element.
Remove an attribute by name
This example uses removeAttribute() to remove the "category" attribute from the first <book> element.
Remove attributes by object
This example uses removeAttributeNode() to remove all attributes from all <book> elements.
The removeChild() method removes a specified node.
When a node is removed, all its child nodes are also removed.
The following code fragment will remove the first <book> element from the loaded xml:
Example explained:
The removeChild() method is the only way to remove a specified node.
When you have navigated to the node you want to remove, it is possible to remove that node using the parentNode property and the removeChild() method:
Example explained:
The removeChild() method can also be used to remove a text node:
Example explained:
It is not very common to use removeChild() just to remove the text from a node. The nodeValue property can be used instead. See next paragraph.
The nodeValue property can be used to change or clear the value of a text node:
Example explained:
Loop through and change the text node of all <title> elements:
Try
it yourself
The removeAttribute(name) method is used to remove an attribute node by its name.
Example: removeAttribute('category')
The following code fragment removes the "category" attribute in the first <book> element:
Example explained:
Loop through and remove the "category" attribute of all <book> elements: Try it yourself
The removeAttributeNode(node) method is used to remove an attribute node, using the node object as parameter.
Example: removeAttributeNode(x)
The following code fragment removes all the attributes of all <book> elements:
Example explained:
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The HTML5 Certificate documents your knowledge of advanced HTML5.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
Your message has been sent to W3Schools.