HTML and CSS Reference
In-Depth Information
use your code to experiment with the various methods listed in Table 1-2 in this function,
starting with
getElementById
.
The
getElementById
method returns the element in the page that matches the specific ID
value you pass to it. It returns null if no element on the page has the specified ID. Each ele-
ment on the page should have a unique ID. For example, if you want to reference the
<div>
element with the ID
outerDiv
, you would use the following code:
var element = document.getElementById("outerDiv");
alert(element.innerHTML);
The JavaScript
alert
method, which displays a message box, is used here to show whether
you have actually accessed the DOM successfully. The
alert
isn't all that useful in the real
world but it is for development purposes. When you run the page, notice the message box
from the browser with all the
innerHTML
contents of the
<div>
you selected out of the DOM
with your code (see Figure 1-14).
FIGURE 1-14
A JavaScript
alert
demonstrating successful access to the DOM
Search WWH ::
Custom Search