W3Schools

home HOME

XML DOM Tutorial
DOM HOME
DOM Introduction
DOM Nodes
DOM Node Tree
DOM Parsing
DOM Load Function
DOM Methods
DOM Accessing
DOM Node Info
DOM Node List
DOM Traversing
DOM Browsers
DOM Navigating

Manipulate Nodes
DOM Get Values
DOM Change Nodes
DOM Remove Nodes
DOM Replace Nodes
DOM Create Nodes
DOM Add Nodes
DOM Clone Nodes
DOM HttpRequest

XML DOM Reference
DOM Node Types
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentImpl
DOM DocumentType
DOM ProcessingInstr
DOM Element
DOM Attribute
DOM Text
DOM CDATA
DOM Comment
DOM HttpRequest
DOM ParseError Obj
DOM Parser Errors

DOM Summary

Examples
DOM Examples
DOM Validator

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Forum

Helping W3Schools

XML DOM Remove Nodes

prev next

The removeChild() method removes a specified node.

The removeAttribute() method removes a specified attribute.


Examples

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.


Remove an Element Node

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:

xmlDoc=loadXMLDoc("books.xml");
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  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

Try it yourself


Remove Myself - Remove the Current Node

The removeChild() method is the only way to removes 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:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  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

Try it yourself


Remove a Text Node

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

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];

y=x.childNodes[0];
x.removeChild(y);

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  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

Try it yourself

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 or clear the value of a text node:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Set the variable x to be the text node of the first title element
  3. Use the nodeValue property to clear the text from the text node

Try it yourself

Loop through and change the text node of all <title> elements: Try it yourself


Remove an Attribute Node by Name

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:

xmlDoc=loadXMLDoc("books.xml");

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

Example explained:

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

Try it yourself

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


Remove Attribute Nodes by Object

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:

xmlDoc=loadXMLDoc("books.xml");

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);
  }
}

Example explained:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  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

Try it yourself


prev next


Ektron
Ektron

The Ektron Intranet lets you do everything you need to do on your corporate intranet and everything you want to do... all with just one application.

What can you do with the Ektron Intranet?

Ektron

Navigate through content, documents, assets, colleagues and workgroups quickly and intuitively with enterprise search

Ektron

Communicate with friends and colleagues with forums, message boards and corporate blogging using the new Social Networking Platform

Ektron

Promote collaboration among coworkers in your organization through project workspaces where others can efficiently find information and work together

Ektron

Personalize your company profile by bookmarking and organizing favorite content, uploading assets, posting photos, blogging, and more

Ektron

Interact with features like tagging, flagging, wikis and ratings found in the Web 2.0 Toolbox

Ektron

Author/edit content, manage navigation, menus, audit trails, workflow and approvals with the best in breed Content Management

See why there are 20,000+ Ektron integrations worldwide.
Request an INSTANT DEMO or download a FREE TRIAL today.




Jump to: Top of Page or HOME or Printer Friendly Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its contents. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2008 by Refsnes Data. All Rights Reserved.

Validate Validate W3C-WAI level A conformance icon W3Schools was converted to XHTML in December 1999
SITE SEARCH
 
About W3Schools
W3Schools Forum
IISProtect
Password Protect
Your Web Pages
WEB HOSTING
$15 Domain Name
Registration
Save $20 / year!
UK Domain Names
UK Web Hosting

Alojamiento Web
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap Web Hosting
Best Web Hosting
Domain Name Registration
PHP MySQL Hosting
Top 10 Web Hosting
Web Hosting Providers
Web Hosting Company
UK Reseller Hosting
Web Hosting
WEB BUILDING
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Custom Programming
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
Ecommerce Software
XMLSpy XML Editor
Azbuz Blog
SHOPPING
UK Wholesalers
UK Wholesale
UAE Dubai Property
Private Student Loans
UK Dropshippers & Wholesalers Directory
EDUCATION
US Web Design Schools
HTML Exam
YELLOW PAGES
www.nettkatalogen.no
www.gulex.dk
www.gulex.se
www.gelbex.de
www.teloos.fi
www.teloos.fr
www.teloos.co.uk
www.teloos.at
www.teloos.de