Latest web development tutorials

XML DOM setAttributeNode () method

Element Object Reference Element object

Definition and Usage

setAttributeNode () method to add a new attribute node.

If the attribute specifies the name of the element already exists, then the property will be a new property instead. If the new attribute replaces an existing attribute, the replaced attribute node, otherwise it returns NULL.

grammar

elementNode.setAttributeNode(att_node)

参数 描述
att_node 必需。规定要设置的属性节点。


Examples

The following code fragment uses loadXMLDoc () to " the Books.xml " into xmlDoc and add "edition" attribute to all <book> element:

Examples

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');
var newatt;

for (i=0;i<x.length;i++)
{
newatt=xmlDoc.createAttribute("edition");
newatt.value="first";
x[i].setAttributeNode(newatt);
}

//Output all "edition" attribute values
for (i=0;i<x.length;i++)
{
document.write("Edition: ");
document.write(x[i].getAttribute("edition"));
document.write("
");
}

Output:

Edition: first
Edition: first
Edition: first
Edition: first

try it"

Element Object Reference Element object