getAttribute
Summary
Returns the value of the content attribute.
Method of dom/Elementdom/Element
Syntax
var attributeValue = element.getAttribute(name);
Parameters
name
- Data-type
- String
The name of the attribute.
Return Value
Returns an object of type StringString
The value of the attribute, or null if it does not exist.
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).
<!doctype html>
<head>
<title>Content/DOM Attribute Example</title>
<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>
Usage
Use this method to get the value of a content attribute of an element.
Related specifications
- Document Object Model (DOM) Level 3 Core
- Recommendation
- Document Object Model (DOM) Level 2 Core
- Recommendation
- Document Object Model (DOM) Level 1
- Recommendation
- DOM
- Living Standard
See also
Related pages
- removeAttributeremoveAttribute
- setAttributesetAttribute
Attributions
Microsoft Developer Network: [Windows Internet Explorer API reference Article]