Modifying the Tree

The document is the owner/parent of all page elements. Because of this, most factory methods to create instances of new elements are methods on the Core document object. The Node, though, maintains the navigation within the Core API. This supports the hierarchical structure of the document tree, in which each node has a relationship to other nodes, and navigation follows this natural structure: parent/child, sibling/sibling. Finally, the Element provides a way to access elements within context in order to apply changes to nested elements. All three are essential objects when it comes to modifying the document tree.

The document factory methods, and the type of Core objects they create, are listed in Table 10-1. This also provides a brief introduction to several of the Core objects.

Table 10-1. Factory methods of the Document object

MethodObject createdDescription
createElement(tagname)
Element
Creates an element that is cast to the specific tag
createDocumentFragment
DocumentFragment
The DocumentFragment is a lightweight document, used when extracting a section of the document tree
createTextNode(data)
Text
Holds any text in the page
createComment(data)
Comment
XML comment
createCDATASection(data)
CDATASection
CDATA section
createProcessingInstructions(target,data)
ProcessingInstruction
XML processing instruction
createAttribute(name)
Attr
Element attribute
createEntityReference(name)
EntityReference
Placeholder for an element to be placed later
createElementNS ...

Get Learning JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.