04.11.2015 Views

javascript

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

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

Chapter 10: The Document Object Model<br />

HTML Elements<br />

All HTML elements are represented by the HTMLElement type, either directly or through subtyping. The<br />

HTMLElement inherits directly from Element and adds several properties. Each property represents one<br />

of the following standard attributes that are available on every HTML element:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

id — A unique identifier for the element in the document.<br />

title — Additional information about the element, typically represented as a tooltip.<br />

lang — The language code for the contents of the element (rarely used).<br />

dir — The direction of the language, “ ltr ” (left - to - right) or “ rtl ” (right - to - left); also rarely<br />

used.<br />

className — The equivalent of the class attribute, which is used to specify CSS classes on an<br />

element. The property could not be named class because class is an ECMAScript reserved<br />

word (see Chapter 1 for information about reserved words).<br />

Each of these properties can be used to both retrieve the corresponding attribute value and to change the<br />

value. Consider the following HTML element:<br />

< div id=”myDiv” class=”bd” title=”Body text” lang=”en” dir=”ltr” > < /div ><br />

All of the information specified by this element may be retrieved using the following JavaScript code:<br />

var div = document.getElementById(“myDiv”);<br />

alert(div.id);<br />

//”myDiv”<br />

alert(div.className); //”bd”<br />

alert(div.title); //”Body text”<br />

alert(div.lang); //”en”<br />

alert(div.dir); //”ltr”<br />

It ’ s also possible to use the following code to change each of the attributes by assigning new values to<br />

the properties:<br />

div.id = “someOtherId”;<br />

div.className = “ft”;<br />

div.title = “Some other text”;<br />

div.lang = “fr”;<br />

div.dir =”rtl”;<br />

Not all of the properties affect changes on the page when overwritten. Changes to id or lang will be<br />

transparent to the user (assuming no CSS styles are based on these values), whereas changes to title<br />

will be apparent only when the mouse is moved over the element. Changes to dir will cause the text on<br />

the page to be aligned either to the left or right as soon as the property is written. Changes to className<br />

may appear immediately if the class has different CSS style information than the previous one.<br />

As mentioned previously, all HTML elements are represented by an instance of HTMLElement or a more<br />

specific subtype. The following table lists each HTML element and its associated type (italicized<br />

elements are deprecated). Note that these types are accessible in Opera, Safari, Chrome, and Firefox via<br />

JavaScript, but not in IE prior to version 8.<br />

280

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

Saved successfully!

Ooh no, something went wrong!