267Chapter 15Generic HTML Element Objects Capture -type click listener: (X web hosting)

267Chapter 15Generic HTML Element Objects Capture -type click listener:

Related Items:attachEvent(), detachEvent(), dispatchEvent(), fireEvent(), removeEventListener()methods. appendChild(elementObject) Returns:Node object reference. Compatibility:WinIE5+, MacIE5+, NN6+, Moz1+, Safari1+ The appendChild()method inserts an element or text node (defined by other code that comesbefore it) as the new, last child of the current element. Aside from the more obvious applicationof adding a new child element to the end of a sequence of child nodes, the appendChild() method is also practical for building element objects and their content before appending, replac- ing, or inserting the element into an existing document. The document.createElement() method generates a reference to an element of whatever tag name you assign as that method sparameter. The appendChild()method returns a reference to the appended node object. This referencediffers from the object that is passed as the method s parameter because the returned valuerepresents the object as part of the document rather than as a freestanding object in memory. ExampleListing 15-21 contains an example that shows how to use the appendChild()method in con- cert with removeChild()and replaceChild()to modify child elements in a document. Because many W3C DOM browsers treat source code carriage returns as text nodes (and thuschild nodes of their parent), the HTML for the affected elements in Listing 15-21 is shownwithout carriage returns between elements. The append()function creates a new lielement and then uses the appendChild()method toattach the text box text as the displayed text for the item. The nested expression, document. createTextNode(form.input.value), evaluates to a legitimate node that is appended tothe new liitem. All of this occurs before the new liitem is added to the document. In thefinal statement of the function, appendChild()is invoked from the vantage point of the ulelement thus adding the lielement as a child node of the ulelement. elementObject.appendChild()

Leave a Reply