The getElementById() method accepts one parameter, this being an id.
"getElementById()" is used to return a reference to an element where the element id attribute matches the value in the parameter.
<div id="mydiv"></div>
|
|
In the above example, we have a div element that has an id attribute "mydiv". To grab the div element, we use the DOM method "getElementById()" and pass the div id "mydiv" as the parameter.
var divObj = document.getElementById("mydiv");
|
|
Now, the variable "divObj" is a reference to the div element. You aren't just limited to div elements, any element that can have an id can be used.
If you have an old javascript book sitting on your desk, it may not explain DOM methods like "getElementById()", it may explain things like "document.all.ID" (Internet Explorer), or "document.ID" (Netscape), but it is common now days to use "getElementById()".
| Discuss Tutorial: Using getElementById() | 1 Comment |