When you're working with the DOM, the ideal way to later access an element on the page is by giving it a unique identifier, or ID. Then, the DOM method document.getElementById() accesses the given element and enables you to go on from there: Modify the element, append subelements, or otherwise further navigate through the DOM tree. In the following example, the <p> element is accessed. Depending on the browser type, the string representation of the element class is different. Whereas Internet Explorer just outputs [object], Mozilla browsers are more verbose and provide more information: [object HTMLParagraphElement]. Accessing an Element By ID (getelementbyid.html)
Warning The DOM is accessible only when the whole document has been loaded. That's the reason the DOM access code is executed only after the load event has been fired. |