Name
setAttributeNode: newAttr
Synopsis
Sets or replaces the attribute in the Node interface’s attributes collection with the given
Attr object. The attribute name
is retrieved from the name attribute of the new attribute object.
If an Attr object with the
given name already exists in the attributes collection, this method
returns a reference to the old Attr object. Otherwise, it returns
null.
Argument
- newAttr: Attr
The new
Attrobject to set.
Exceptions
- WRONG_DOCUMENT_ERR
Raised if the
newAttrnode was created in a document different than the parent node.- NO_MODIFICATION_ALLOWED_ERR
Raised if the new parent node is read-only.
- INUSE_ATTRIBUTE_ERR
Raised if another
Elementalready uses the newAttrnode. Each element must have a distinctAttrobject.
Java binding
public Attr setAttributeNode(Attr newAttr) throws DOMException;
Java example
// Make sure you have an id attribute to work with
Attr attr;
if ((attr = elem.getAttributeNode("id")) = = null) {
// add a default, unique id
attr = doc.createAttribute("id");
elem.setAttributeNode(attr);
// continue processing
}Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access