How to remove attribute method in XML DOM
In this articles i am going to explain about how to remove an attribute by name and object.
How to Remove an Attribute Node by Name
- The removeAttribute() method removes a specified attribute.
- The removeAttribute(name) method is remove name in attribute node.
Example
In this example, we remove the category attribute:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="loadxmldoc.html">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("library.xml");
a = xmlDoc.getElementsByTagName('book');
document.write(a[0].getAttribute('category'));
document.write("<br />");
a[0].removeAttribute('category');
document.write(a[0].getAttribute('category'));
</script>
</body> </html>
|
Define the code
- "loadxmldoc.html" get the data.
- Set 'a' variable to be the first title element node
- Use getElementsByTagName() to get book nodes
- Remove the "category" getattribute form the first book element node
How to Remove an Attribute Node by object
- The removeAttribute() method removes a specified attribute.
- The node object is use to remove an attribute node from removeAttributeNode() method.
Example
In this example, we remove all attributes in the element node by using object.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="loadxmldoc.html">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("library.xml");
a = xmlDoc.getElementsByTagName('book');
for (i = 0; i < a.length; i++) {
while (a[i].attributes.length > 0) {
attnode = a[i].attributes[0];
old_att = a[i].removeAttributeNode(attnode);
document.write("Removed: " + old_att.nodeName)
document.write(": " + old_att.nodeValue)
document.write("<br />")
}
}
</script>
</body>
</html>
|
Define the code
- "loadxmldoc.html" get the data.
- Use getElementsByTagName() to get book nodes
- RemoveAttributeNode() method is removed all attributes in the element node.
You may also want to read these related articles :
Ask Your Question
Got a programming related question? You may want to post your question here
Programming Answers here