Attr, attribute | NN 6 IE 5 DOM 1 | ||||
An abstract representation of an element's attribute name/value pair is an object known in the W3C DOM vernacular as the Attr object; in IE terminology, it is called an attribute object. They are different names for the same object. An attribute object is created in both environments via the document.createAttribute() method; the reference to the attribute object then becomes the parameter to an element's setAttributeNode() method to insert that attribute object into the element. For example: var newAttr = document.createAttribute("author"); newAttr.value = "William Shakespeare"; document.getElementById("hamlet").setAttributeNode(newAttr); |
|||||
Some W3C DOM element methods (most notably, the getAttributeNode() method) return attribute objects, which have properties that may be accessed like any scriptable object. |
|||||
In the W3C DOM abstract model, the Attr object inherits all properties and methods of the Node object. Some Node object properties, however, are not inherited by the attribute object in IE/Windows until Version 6, even though they are implemented for element and text nodes in Version 5. |
|||||
HTML Equivalent | |||||
Any name/value pair inside a start tag. |
|||||
Object Model Reference | |||||
[window.]document.getElementById("elementID").attributes[i] [window.]document.getElementById("elementID").attributes.item(i) [window.]document.getElementById("elementID").attributes.getNamedItem[attrName] |
|||||
Object-Specific Properties | |||||
|
|||||
Object-Specific Methods | |||||
None. |
|||||
Object-Specific Event Handler Properties | |||||
None. |
expando | NN n/a IE 6 DOM n/a |
Read-only | |
Returns Boolean true if the attribute, once it is inserted into an element, is not one of the native attributes for the element. This property is false for an attribute created by document.createAttribute() until the attribute is added to the element (via the setAttributeNode( ) method), at which time the property's value is reevaluated within the context of the element's native attributes. |
|
Example | |
var isCustomAttr = document.getElementById("book3928").getAttributeNode("author").expando; |
|
Value | |
Boolean value: true | false. |
|
Default | |
false |
name | NN 6 IE 5 DOM 1 |
Read-only | |
This is the name portion of the name/value pair of the attribute. It is identical to the nodeName property of the Attr node. You may not modify the name of an attribute by script because other dependencies may lead to document tree confusion. Instead, replace the old attribute with a newly created one, the name of which is a required parameter of the document.createAttribute() method. |
|
Example | |
if (myAttr.name == "author") { // process author attribute } |
|
Value | |
String value. |
|
Default | |
Empty string, although creating a new attribute requires a name. |
ownerElement | NN 6 IE n/a DOM 2 |
Read-only | |
Refers to the element that contains the current attribute object. Until a newly created attribute is inserted into an element, this property is null. |
|
Example | |
if (myAttr.ownerElement.tagName == "fred") { // process attribute of <fred> element } |
|
Value | |
Element node reference. |
|
Default | |
null |
specified | NN 6 IE 5 DOM 1 |
Read-only | |
Returns Boolean true if the value of the attribute is explicitly assigned in the source code or adjusted by script. If the browser reflects an attribute that is not explicitly set (IE does this), the specified property for that value is false, even though the attribute may have a default value determined by the document's DTD. The W3C DOM Level 2 indicates that the specified property of a freshly created Attr object should be true, but both IE 6 and Netscape 6.2 and later leave it false until the attribute is inserted into an element. |
|
Example | |
if (myAttr.specified) { // process attribute whose value is something other than DTD default } |
|
Value | |
Boolean value: true | false. |
|
Default | |
false |
value | NN 6 IE 6 DOM 1 |
Read/Write | |
Provides the value portion of the name/value pair of the attribute. Identical to the nodeValue property of the Attr node, as well as data accessed more directly via an element's getAttribute() and setAttribute() methods. If you create a new attribute object, you can assign its value via the value property prior to inserting the attribute into the element. Attribute node values are always strings, including in IE, which otherwise allows Number or Boolean data types for the corresponding properties. |
|
Example | |
document.getElementById("hamlet").getAttributeNode("author").value = "Shakespeare"; |
|
Value | |
String value. |
|
Default | |
Empty string, except in IE/Windows, which returns the string undefined (that is, not a value whose type evaluates to the undefined value). |