getAttribute method
Retrieves the value of the specified attribute.
Syntax
object.getAttribute(strAttributeName, lFlags)Parameters
- strAttributeName [in]
-
Type: String
String that specifies the name of the attribute.
- lFlags [in, optional]
-
Type: Integer
Integer that specifies one or more of the following flags:
-
Default. Performs a property search that is not case-sensitive, and returns an interpolated value if the property is found.
-
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.
-
Returns attribute value as a String. This flag does not work for event properties.
-
Returns attribute value as a fully expanded URL. Only works for URL attributes.
Return value
Type: Variant
Variant 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
.
Standards information
- Document Object Model (DOM) Level 2 HTML Specification, Section 1.6.5
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.
The strAttributeName parameter requires the name of the desired content attribute and not the DOM attribute.
For example, this method does not require strAttributeName to be "className"
when setting, getting, or removing a CLASS attribute.
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()');
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.
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 type="text/javascript"> 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=email
- input type=file
- input type=hidden
- input type=image
- input type=number
- input type=password
- input type=radio
- input type=range
- input type=reset
- input type=search
- input type=submit
- input type=tel
- input type=text
- input type=url
- 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