THE WORLD'S LARGEST WEB DEVELOPER SITE
×

XML Tutorial

XML HOME XML Introduction XML How to use XML Tree XML Syntax XML Elements XML Attributes XML Namespaces XML Display XML HttpRequest XML Parser XML DOM XML XPath XML XSLT XML XQuery XML XLink XML Validator XML DTD XML Schema XML Server XML Examples XML Quiz XML Certificate

XML AJAX

AJAX Introduction AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

XML DOM

DOM Introduction DOM Nodes DOM Accessing DOM Node Info DOM Node List DOM Traversing DOM Navigating DOM Get Values DOM Change Nodes DOM Remove Nodes DOM Replace Nodes DOM Create Nodes DOM Add Nodes DOM Clone Nodes DOM Examples

XPath Tutorial

XPath Introduction XPath Nodes XPath Syntax XPath Axes XPath Operators XPath Examples

XSLT Tutorial

XSLT Introduction XSL Languages XSLT Transform XSLT <template> XSLT <value-of> XSLT <for-each> XSLT <sort> XSLT <if> XSLT <choose> XSLT Apply XSLT on the Client XSLT on the Server XSLT Edit XML XSLT Examples

XQuery Tutorial

XQuery Introduction XQuery Example XQuery FLWOR XQuery HTML XQuery Terms XQuery Syntax XQuery Add XQuery Select XQuery Functions

XML DTD

DTD Introduction DTD Building Blocks DTD Elements DTD Attributes DTD Elements vs Attr DTD Entities DTD Examples

XSD Schema

XSD Introduction XSD How To XSD <schema> XSD Elements XSD Attributes XSD Restrictions

XSD Complex

XSD Elements XSD Empty XSD Elements Only XSD Text Only XSD Mixed XSD Indicators XSD <any> XSD <anyAttribute> XSD Substitution XSD Example

XSD Data

XSD String XSD Date XSD Numeric XSD Misc XSD Reference

Web Services

XML Services XML WSDL XML SOAP XML RDF XML RSS

References

DOM Node Types DOM Node DOM NodeList DOM NamedNodeMap DOM Document DOM Element DOM Attribute DOM Text DOM CDATA DOM Comment DOM XMLHttpRequest DOM Parser XSLT Elements XSLT/XPath Functions

XML DOM Remove Nodes


The removeChild() method removes a specified node.

The removeAttribute() method removes a specified attribute.


Try it Yourself - Examples

The examples use the XML file books.xml.

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.

×

Header


Remove an Element Node

The removeChild() method removes a specified node.

When a node is removed, all its child nodes are also removed.

This code will remove the first <book> element from the loaded xml:

Example

y = xmlDoc.getElementsByTagName("book")[0];

xmlDoc.documentElement.removeChild(y);
Try it Yourself »

Example explained:

  1. Suppose "books.xml" is loaded into xmlDoc
  2. Set the variable y to be the element node to remove
  3. Remove the element node by using the removeChild() method from the parent node


Remove Myself - Remove the Current Node

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

x = xmlDoc.getElementsByTagName("book")[0];

x.parentNode.removeChild(x);
Try it Yourself »

Example explained:

  1. Suppose "books.xml" is loaded into xmlDoc
  2. Set the variable y to be the element node to remove
  3. Remove the element node by using the parentNode property and the removeChild() method

Remove a Text Node

The removeChild() method can also be used to remove a text node:

Example

x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
x.removeChild(y);
Try it Yourself »

Example explained:

  1. Suppose "books.xml" is loaded into xmlDoc
  2. Set the variable x to be the first title element node
  3. Set the variable y to be the text node to remove
  4. Remove the element node by using the removeChild() method from the parent node

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.


Clear a Text Node

The nodeValue property can be used to change the value of a text node:

Example

xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue = "";
Try it Yourself »

Example explained:

  1. Suppose "books.xml" is loaded into xmlDoc
  2. Get the first title element's first child node.
  3. Use the nodeValue property to clear the text from the text node

Remove an Attribute Node by Name

The removeAttribute() method removes an attribute node by its name.

Example: removeAttribute('category')

This code removes the "category" attribute in the first <book> element:

Example

x = xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
Try it Yourself »

Example explained:

  1. Suppose "books.xml" is loaded into xmlDoc
  2. Use getElementsByTagName() to get book nodes
  3. Remove the "category" attribute form the first book element node

Loop through and remove the "category" attribute of all <book> elements: Try it yourself


Remove Attribute Nodes by Object

The removeAttributeNode() method removes an attribute node, using the node object as parameter.

Example: removeAttributeNode(x)

This code removes all the attributes of all <book> elements:

Example

x = xmlDoc.getElementsByTagName("book");

for (i = 0; i < x.length; i++) {
    while (x[i].attributes.length > 0) {
        attnode = x[i].attributes[0];
        old_att = x[i].removeAttributeNode(attnode);
    }
}
Try it Yourself »

Example explained:

  1. Suppose "books.xml" is loaded into xmlDoc
  2. Use getElementsByTagName() to get all book nodes
  3. For each book element check if there are any attributes
  4. While there are attributes in a book element, remove the attribute