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:
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: 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
The strAttributeName parameter requires the name of the desired content attribute and not the Document Object Model (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).
<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
- area
- b
- base
- blockQuote
- body
- br
- button
- caption
- cite
- code
- col
- colGroup
- comment
- currentStyle
- custom
- dd
- div
- dl
- dt
- em
- embed
- fieldSet
- form
- 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
- map
- meta
- object
- ol
- option
- p
- pre
- runtimeStyle
- s
- samp
- script
- select
- small
- span
- strong
- style
- sub
- sup
- table
- tBody
- td
- textArea
- tFoot
- th
- tHead
- title
- tr
- u
- ul
- var
- removeAttribute
- setAttribute
Build date: 11/28/2012
