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 />

necessary when the attribute is accessed as an object property). If the attribute with the given name<br />

doesn ’ t exist, getAttribute() always returns null .<br />

The getAttribute() method can also retrieve the value of custom attributes that aren ’ t part of the<br />

formal HTML language. Consider the following element:<br />

< div id=”myDiv” my_special_attribute=”hello!” > < /div ><br />

In this element, a custom attribute named my_special_attribute is defined to have a value of<br />

“ hello! ” . This value can be retrieved using getAttribute() just like any other attribute, as shown<br />

here:<br />

var value = div.getAttribute(“my_special_attribute”);<br />

Note that the attribute name is case - insensitive, so “ ID ” and “ id ” are considered the same attribute.<br />

All attributes on an element are also accessible as properties of the DOM element object itself. There are,<br />

of course, the five properties defined on HTMLElement that map directly to corresponding attributes, but<br />

all recognized (noncustom) attributes get added to the object as properties. Consider the following<br />

element:<br />

< div id=”myDiv” align=”left” my_special_attribute=”hello” > < /div ><br />

Since id and align are recognized attributes for the < div > element in HTML, they will be represented<br />

by properties on the element object. The my_special_attribute attribute is custom, and so won ’ t<br />

show up as a property on the element in Safari, Opera, Chrome, or Firefox. IE creates properties for<br />

custom attributes as well, as this example demonstrates:<br />

alert(div.id);<br />

//”myDiv”<br />

alert(div.my_special_attribute); //undefined (except in IE)<br />

alert(div.align);<br />

//”left”<br />

Two types of attributes have property names that don ’ t map directly to the same value returned by<br />

getAttribute() . The first attribute is style , which is used to specify stylistic information about the<br />

element using CSS. When accessed via getAttribute() , the style attribute contains CSS text while<br />

accessing it via a property that returns an object. The style property is used to programmatically access<br />

the styling of the element (discussed later in this chapter) and so does not map directly to the style<br />

attribute.<br />

The second category of attribute that behaves differently is event - handler attributes such as onclick .<br />

When used on an element, the onclick attribute contains JavaScript code, and that code string is<br />

returned when using getAttribute() .When the onclick property is accessed, however, it returns a<br />

JavaScript function (or null if the attribute isn ’ t specified). This is because onclick and other event -<br />

handling properties are provided such that functions can be assigned to them.<br />

Due to these differences, developers tend to forego getAttribute() when programming the DOM in<br />

JavaScript and instead use the object properties exclusively. The getAttribute() method is used<br />

primarily to retrieve the value of a custom attribute.<br />

283

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

Saved successfully!

Ooh no, something went wrong!