180Part IIIDocument Objects ReferenceAny element that you intend (Web hosting domains)

180Part IIIDocument Objects ReferenceAny element that you intend to script whether to change its content or its style shouldhave an identifier assigned to the element s idattribute. Form control elements still requirenameattributes if you submit the form content to a server. But you can freely assign a differ- ent identifier to a control s idattribute. Scripts can use either the idor the document. formReference.elementNamereference to reach a control object. Identifiers are essen- tially the same as the values you assign to the nameattributes of form and form input ele- ments. Following the same rules for the nameattribute value, an ididentifier must be asingle word (no white space), it cannot begin with a numeral (to avoid conflicts inJavaScript), and it should avoid punctuation symbols except for the underscore. While anelement can be accessed by numeric index within the context of some surrounding element(such as the body), this is a risky practice when content is under construction. Uniqueidentifiers make it much easier for scripts to reference objects and are not affected bychanges in content order. New DOM conceptsWith the W3C DOM come several concepts that may be entirely new to you unless you haveworked extensively with the terminology of tree hierarchies. Concepts that have the mostimpact on your scripting are new ways of referencing elements and nodes. Element referencingScript references to objects in the DOM Level 0 are observed in the W3C DOM for backwardcompatibility. Therefore, a form input element whose nameattribute is assigned the valueuserNameis addressed just like it always is: document.forms[0].userNameordocument.formName.userNameBut because all elements of a document are exposed to the documentobject, you can use thenew documentobject method to access any element whose ID is assigned. The method isdocument.getElementById(), and the sole parameter is a string version of the identifier ofthe object whose reference you wish to get. To help put this in context with what you mayhave used with the IE4 object model, consider the following HTML paragraph tag:

In IE4+, you can reference this element withvar elem = document.all.myParagraph; Although the document.allcollection is not implemented in the W3C DOM, use the newdocumentobject method (available in IE5+, Moz1+, Safari, and others) that enables you toaccess any element by its ID: var elem = document.getElementById( myParagraph ); Unfortunately for scripters, this method is difficult to type since it is case-sensitive, so watchout for that ending lowercase d . A hierarchy of nodesThe issue surrounding containers (described earlier) comes into play for the underlyingarchitecture of the W3C DOM. Every element or freestanding chunk of text in an HTML (orXML) document is an object that is contained by its next outermost container. Let s look at a

Leave a Reply