Returns a reference to the first object with the specified
value of the ID or NAME attribute.
Syntax
oElement = object.getElementById(sIDValue)
Parameters
| sIDValue |
Required.
A String that specifies the ID value. Case-insensitive. |
Return Value
Returns the first object with the specified ID or NAME.
Remarks
Internet Explorer 8 and later. In IE8 mode, getElementById performs a case-sensitive match on the ID attribute only. In IE7 mode and previous modes, this method performs a case-insensitive match on both the ID and NAME attributes, which might produce unexpected results. For more information, see Defining Document Compatibility.
If more than one element is found, getElementById returns the first object in the collection.
Example
The following example uses getElementById to display the content of the first div element in a collection with the ID attribute value, oDiv1, when a button is clicked.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>"Show First DIV Content"</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<meta http-equiv="X-UA-Compatible" content="IE=8">
<script type="text/javascript">
function fnGetId(){
// Display the content of the first DIV element in the collection.
var oVDiv=document.getElementById("oDiv1");
alert("The content of the first DIV element is " + "\"" + oVDiv.innerHTML + "\".");
}
</script>
</head>
<body>
<div id="oDiv1">Div #1</div>
<div id="oDiv2">Div #2</div>
<div id="oDiv3">Div #3</div>
<input type="button" value="Show First DIV Content" onclick="fnGetId()">
</body>
</html>
Standards Information
This method is defined in
World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1
.
Applies To
See Also
About the W3C Document Object Model