[This documentation is preliminary and is subject to change.]
Retrieves the value of the specified attribute.
![]() |
Syntax
object.getAttribute(strAttributeName, lFlags)Standards information
- Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5
Parameters
- strAttributeName [in]
-
Type: BSTR
String that specifies the name of the attribute.
- lFlags [in, optional]
-
Type: LONG
Integer that specifies one or more of the following flags:
0
-
Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found.
1
-
Performs a case-sensitive property search. To find a match, the uppercase and lowercase letters in strAttributeName must exactly match those in the attribute name.
2
-
Returns attribute value as a String. This flag does not work for event properties.
4
-
Returns attribute value as a fully expanded URL. Only works for URL attributes.
Return value
Type: VariantVariant that returns a String, Variant of type Integer, or Boolean value as defined by the attribute. If the attribute is not present, this method returns null.
Remarks
Windows Internet Explorer 8 and later. IE8 Standards mode enables several enhancements to the setAttribute, getAttribute, and removeAttribute methods that are not available when pages are displayed in earlier document modes.
- The strAttributeName parameter requires the name of the desired content attribute and not the Document Object Model (DOM) attribute.
For example, in IE8 mode, this method no longer requires strAttributeName to be
"className"when setting, getting, or removing a CLASS attribute. Earlier versions of Windows Internet Explorer and Internet Explorer 8 in compatibility mode still require strAttributeName to specify the corresponding DOM property name. - The strAttributeName parameter is not case sensitive. As a result, the lFlags parameter is no longer supported and should not be used.
- The methods support event handlers. For example, the following code example defines an event handler to call a function called SomeFunction when the body of the page is loaded.
document.body.setAttribute('onload', 'SomeFunction()');
Internet Explorer 8 or later. In IE8 mode, the value property is correctly reported as a canonical attribute name. For example, <input type="text" readonly> and <input type="text" readonly="readonly"> would both set the input text field to read-only. In IE8 mode, the value is evaluated as a canonical value, "readonly". In IE7 and earlier modes, it is evaluated as a Boolean value, true. For more information, see Attribute Differences in Internet Explorer 8
If two or more attributes have the same name (differing only in uppercase and lowercase letters) and lFlags is 0, the getAttribute method retrieves values only for the last attribute created with this name, and ignores all other attributes with the same name.
Internet Explorer 8 and later. In IE7 Standards mode and IE5 (Quirks) mode, attributes that represent URLs and file paths return the same value whether you retrieve them as a DOM property or as a content attribute (using getAttribute). Some attributes return relative URLs, whereas others always return a fully qualified URL. In IE8 mode, DOM attributes always return fully qualified URLs for URL attributes; to retrieve the attribute value as specified in the content, use getAttribute. The following attributes return URLs: action, background, BaseHref, cite, codeBase, data, dynsrc, href, longDesc, lowsrc, pluginspage, profile, src, url, and vrml.
Examples
The following example shows the difference between a URL attribute which is using a content attribute (value from getAttribute) and a DOM attribute (property).
Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/getAttribute8.htm
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<title>Content/DOM Attribute Example</title>
<base href="http://msdn.microsoft.com/">
<script>
function GetAttr(){
//Retrieve an element value from the content attribute and DOM attribute.
var o = document.getElementById("msdn");
var sContentAttr = o.getAttribute("href");
var sDomAttr = o.href;
var sOutput = ("Content attribute value: " + sContentAttr +
"<br/>DOM attribute value: " + sDomAttr);
document.getElementById("output").innerHTML = sOutput;
}
</script>
</head>
<body onload="GetAttr()">
<a id="msdn" href="en-us/default.aspx">Microsoft Developer Network</a>
<div id="output">Output appears here.</div>
</body>
See also
- a
- address
- applet
- area
- b
- base
- baseFont
- bgSound
- big
- blockQuote
- body
- br
- button
- caption
- center
- cite
- code
- col
- colGroup
- comment
- currentStyle
- custom
- dd
- dfn
- dir
- div
- dl
- dt
- em
- embed
- fieldSet
- font
- form
- frame
- frameSet
- head
- hn
- hr
- html
- i
- iframe
- img
- input type=button
- input type=checkbox
- input type=file
- input type=hidden
- input type=image
- input type=password
- input type=radio
- input type=reset
- input type=submit
- input type=text
- kbd
- label
- legend
- li
- link
- listing
- map
- marquee
- menu
- meta
- nextID
- noBR
- object
- ol
- option
- p
- plainText
- pre
- runtimeStyle
- 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
- xmp
- removeAttribute
- setAttribute
Build date: 3/8/2012
is src considered "URL attribute" ? I hoped so, but MSIE returns raw form of src.
Be aware that there is an obscure bug in this method when used on a form element containing an input element which has the name attribute set to "id".
Example:
<form id="myForm1">
<input id="user_id" name="user_id" value="text" />
</form>
<form id="myForm2">
<input id="id" name="id" value="text" />
</form>
<script type="text/javascript">
var formElement1 = document.getElementById('myForm1');
var formElement2 = document.getElementById('myForm2');
alert(formElement1.getAttribute('id')+ "\n" + formElement2.getAttribute('id'));
</script>
You would expect it to display a JavaScript alert box containing:
myForm1
myForm2
But instead you get:
myForm1
[object]
It appears that this method erroneously returns the input with the name "id" instead of the value of the id attribute of the form itself. This also happens if you use formElement2.id .
To work around the bug, use attributes['id'].value or getAttributeNode('id').value
See: http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes (jmaxwilson)
----
[jsudds.msft] By way of explanation, this behavior is due in part to the adding the name attribute as an expando property of the <form> object. In this case, the name attribute overwrites a well-known property (id) in the DOM. The same thing would happen to any of the other properties if a well-known name was used, such as <input name='className' />. The getAttribute method in IE searches for property values using expandos and COM properties rather than the attribute node, as does setAttribute(). This combination of factors can lead to unexpected results.
