getAttributeNode method
Returns the attribute node with the specified name from the current element.
To get the value of an attribute, you do not need to work with attribute nodes; the use of the getAttribute method is simpler.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
Required. String that specifies the name of the attribute.
The name is case-sensitive in XML documents and case-insensitive in HTML documents.
If you want to get an HTML attribute, use the name of the HTML attribute instead of the corresponding JavaScript property name in Internet Explorer (unlike in the case of the getAttribute method)!
|
Return value:
If no attribute is specified with the given name, it returns null, else it returns the matching attribute node.
In Internet Explorer before version 8, if an attribute has a default value and the attribute is not specified, then the getAttributeNode method retrieves an attribute node representing the default value.
If you want to detect whether an element has an attribute with a specified name, use the specified property of the returned attribute node.
Methods for attributes without namespaces:
Name | Browser | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
createAttribute | Creates a new attribute node with the specified name. | |||||||||||
getAttribute | Returns the value of the attribute with the specified name from the current element. | |||||||||||
getAttributeNode | Returns the attribute node with the specified name from the current element. | |||||||||||
getNamedItem | Returns the attribute node with the specified name from the current attributes collection. | |||||||||||
hasAttribute |
|
Returns whether the current element has an attribute with the specified name or not. | ||||||||||
removeAttribute | Removes the attribute with the specified name from the current element. | |||||||||||
removeAttributeNode | Removes the specified attribute node from the current element. | |||||||||||
removeNamedItem | Removes the attribute with the specified name from the current attributes collection and returns the removed attribute node. | |||||||||||
setAttribute | Adds an attribute with the specified name and value to the current element. | |||||||||||
setAttributeNode | Adds the specified attribute node to the current element. | |||||||||||
setNamedItem | Adds the specified attribute node to the current attributes collection. |
Example HTML code 1:
This example illustrates the use of the getAttributeNode method:
|
||||
<head> <style> .red {color:red;} .green {color:green;} </style> <script type="text/javascript"> function SetClass () { var div = document.getElementById ("myDiv"); var attr = div.getAttributeNode ("class"); attr.value = "green"; } </script> </head> <body> <div class="red" id="myDiv"> Some text inside the division element. </div> <br /> <button onclick="SetClass ()">Set a style class for the division element!</button> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
CommentNode
HTML elements:
a, abbr, acronym, address, applet, area, b, base, basefont, bdo, bgsound, big, blink, blockquote, body, br, button, caption, center, cite, code, col, colgroup, comment, dd, del, dfn, dir, div, dl, dt, em, embed, fieldset, font, form, frame, frameset, h1, h2, h3, h4, h5, h6, head, hr, html, i, iframe, img, input:button, input:checkbox, input:file, input:hidden, input:image, input:password, input:radio, input:range, input:reset, input:search, input:submit, input:text, ins, isindex, kbd, keygen, label, legend, li, link, listing, map, marquee, menu, meta, nobr, noframes, noscript, object, ol, optgroup, option, p, param, plaintext, pre, q, rt, ruby, s, samp, script, select, small, span, strike, strong, style, sub, sup, table, tbody, td, textarea, tfoot, th, thead, title, tr, tt, u, ul, var, wbr, xml, xmp
Related pages:
getAttributeNodeNS
createAttribute
getAttribute
getNamedItem
hasAttribute
removeAttribute
removeAttributeNode
removeNamedItem
setAttribute
setAttributeNode
setNamedItem
createAttribute
getAttribute
getNamedItem
hasAttribute
removeAttribute
removeAttributeNode
removeNamedItem
setAttribute
setAttributeNode
setNamedItem
External links:
User Contributed Comments