Easy Tutorial
❮ Dom Summary Met Text Insertdata ❯

XML DOM Remove Node


The removeChild() method removes the specified node.

The removeAttribute() method removes the specified attribute.


Try It Yourself - Examples

The following examples use the XML file books.xml. The loadXMLDoc() function, located in an external JavaScript, is used to load the XML file.

Remove Element Node

Remove Current Element Node

Remove Text Node

Clear Text Node's Text

Remove Attribute by Name

Remove Attribute by Object


Remove Element Node

The removeChild() method removes the specified node.

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

The following code snippet will remove the first <book> element from the loaded XML:

Example

Example Explanation:


Remove Itself - Remove the Current Node

The removeChild() method is the only method that can remove a specified node.

When you have navigated to the node you want to remove, you can remove it by using the parentNode property and the removeChild() method:

Example

Example Explanation:


Remove Text Node

The removeChild() method can be used to remove text nodes:

Example

Example Explanation:

It is less common to use removeChild() to remove text from a node. You can use the nodeValue property instead. See the next section.


Clear Text Node

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

Example

Example Explanation:

Traverse and change the text nodes of all <title> elements: Try It Yourself


Remove Attribute Node by Name

The removeAttribute(name) method is used to remove an attribute node by name.

Example: removeAttribute('category')

The following code snippet removes the "category" attribute from the first <book> element:

Example

Example Explanation:

Traverse and remove the "category" attribute from all <book> elements: Try It Yourself


Remove Attribute Node by Object

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

Example: removeAttributeNode(x)

The following code snippet removes all attributes from all <book> elements:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book");

for (i=0;i&lt;x.length;i++)
{
  while (x[i].attributes.length>0)
  {
    attnode=x[i].attributes[0];
    old_att=x[i].removeAttributeNode(attnode);
  }
}

Example Explanation:

❮ Dom Summary Met Text Insertdata ❯