Accessing Specific Elements


document.getElementById("para") 

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)

<script language="JavaScript"   type="text/javascript"> window.onload = function() {   window.alert(document.getElementById("para")); } </script> <p >JavaScript Phrasebook</p> 

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.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net