Web site designers - 269Chapter 15Generic HTML Element ObjectsThe final part of
269Chapter 15Generic HTML Element ObjectsThe final part of the demonstration uses the removeChild()method to peel away all children ofthe ulelement until just the two original items are left standing. Again, the lastChildpropertycomes in handy as the restore()function keeps removing the last child until only two remain. Related Items:removeChild(), replaceChild()methods; nodes and children (Chapter 14). applyElement(elementObject[, type]) Returns:Nothing. Compatibility:WinIE5+, MacIE-, NN-, Moz-, Safari- The applyElement()method enables you to insert a new element as the parent or child of thecurrent object. An important feature of this method is that the new object is wrapped around thecurrent object (if the new element is to become the parent) or the current object s content (if thenew element is to become a child). When the new element becomes a child, all previous childrenare nested further by one generation to become immediate children of the new element. You canimagine how the resulting action of this method affects the containment hierarchy of the currentelement, so you must be careful in how you use the applyElement()method. One parameter, a reference to the object to be applied, is required. This object may be gener- ated from constructions such as document.createElement()or from one of the child ornode methods that returns an object. The second parameter is optional, and it must be one ofthe following values: Parameter ValueDescriptionOutsideNew element becomes the parent of the current objectInsideNew element becomes the immediate child of the current objectIf you omit the second parameter, the default value (outside) is assumed. Listing 15-22shows how the applyElement()method is used both with and without default values. ExampleTo help you visualize the impact of the applyElement()method with its different parametersettings, Listing 15-22 enables you to apply a new element (an emelement) to a spanelementinside a paragraph. At any time, you can view the HTML of the entire pelement to see wherethe emelement is applied as well as its impact on the element containment hierarchy for theparagraph. After you load the page, inspect the HTML for the paragraph before doing anything else. Notice the spanelement and its nested fontelement, both of which surround the one-wordcontent. If you apply the emelement inside the spanelement (click the middle button), thespanelement s first (and only) child element becomes the emelement; the fontelement isnow a child of the new emelement. Listing 15-22: Using the applyElement() Method
ContinuedelementObject.applyElement()