XML DOM Set Nodes![]() ![]() ExamplesIn the examples below, we will use the XML file books.xml, and the JavaScript function loadXMLDoc(). Set a new attribute and attribute value Create a new attribute node Change an attribute‘s value Change an item‘s value Set a New Attribute and Attribute ValueThe setAttribute() method can be used to change the value of an existing attribute, or to create a new attribute/attribute value for an element. The following code fragment adds a new attribute/attribute value to each <book> element:
Another Way to Create a New AttributeThe createAttribute() is used to create a new attribute node. The following code fragment uses createAttribute() to create a new attribute node, and setAttributeNode() to insert it to an element.:
Change an Attribute ValueThe setAttribute() method can be used to change the value of an existing attribute, or to create a new attribute/attribute value for an element. The following code fragment changes the value of the existing "category" attribute (in each <book> element):
Change an Item‘s ValueThe getNamedItem() method can be used to change the value of an existing item. The following code fragment also changes the value of the existing "category" attribute (in each <book> element):
|
|