15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

250<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

getElementById(“elementID”)<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓<br />

Example<br />

You can find many examples of this method in use throughout this book, but you<br />

can take a closer look at how it works by experimenting in The Evaluator (Chapter<br />

13 in the <strong>JavaScript</strong> <strong>Bible</strong>). A number of elements in The Evaluator have IDs<br />

assigned to them, so that you can use the method to inspect the objects and their<br />

properties. Enter the following statements into both the top and bottom text fields<br />

of The Evaluator. Results from the top field are references to the objects; results<br />

from the bottom field are lists of properties for the particular object.<br />

document.getElementById(“myP”)<br />

document.getElementById(“myEM”)<br />

document.getElementById(“myTitle”)<br />

document.getElementById(“myScript”)<br />

As you see in the Results field, NN6 is more explicit about the type of HTML element<br />

object being referenced in the top text field than IE5. But nevertheless, both<br />

browsers are pointing to the same objects.<br />

getElementsByName(“elementName”)<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓<br />

Example<br />

Use The Evaluator to test out the getElementsByName() method. All form elements<br />

in the upper part of the page have names associated with them. Enter the following<br />

statements into the top text field and observe the results:<br />

document.getElementsByName(“output”)<br />

document.getElementsByName(“speed”).length<br />

document.getElementsByName(“speed”)[0].value<br />

You can also explore all of the properties of the text field by typing the following<br />

expression into the bottom field:<br />

document.getElementsByName(“speed”)[0]<br />

document.getElementsByName()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!