Creating and Inserting Elements Using insertBefore and appendChild
The next two table rows allow the user to create a new element and insert it
before the current node or as a child of the current node. The second row (lines
141–145) allows the user to enter text into the text field and click the
InsertBefore button. The text is placed in a new paragraph element, which is
then inserted into the document before the currently selected element, as in Fig.
. The button in lines 143–144 calls the insert
function, defined in
Lines 44–45 call the function createNewNode, passing it the value of the
input field (whose id is ins) as an argument. Function createNewNode, defined in
lines 94–103, creates a paragraph node containing the text passed to it. Line 96
creates a p element using the document object's createElement method. The
createElement method creates a new DOM node, taking the tag name as an argument.
Note that while createElement creates an element, it does not insert the element
on the page.
Line 97 creates a unique id for the new element by concatenating "new" and
the value of idcount before incrementing idcount in line 98. Line 99 assigns the
id to the new element. Line 100 concatenates the element's id in square brackets
to the beginning of text (the parameter containing the paragraph's text).
Line 101 introduces two new methods. The document's createTextNode method
creates a node that can contain only text. Given a string argument,
createTextNode inserts the string into the text node. In line 101, we create a
new text node containing the contents of variable text. This new node is then
used (still in line 101) as the argument to the appendChild method, which is
called on the paragraph node. Method appendChild is called on a parent node to
insert a child node (passed as an argument) after any existing children.
After the p element is created, line 102 returns the node to the calling
function insert, where it is assigned to variable newNode in lines 44–45. Line
46 inserts the newly created node before the currently selected node. The
parentNode property of any DOM node contains the node's parent. In line 46, we
use the parentNode property of current-Node to get its parent.
We call the insertBefore method (line 46) on the parent with newNode and
currentNode as its arguments to insert newNode as a child of the parent directly
before currentNode. The general syntax of the insertBefore method is
parent.insertBefore( newChild, existingChild );
The method is called on a parent with the new child and an existing child as
arguments. The node newChild is inserted as a child of parent directly before
existingChild. Line 47 uses the switchTo function (discussed earlier in this
section) to update the currentNode to the newly inserted node and highlight it
in the XHTML page.
The third table row (lines 145–149) allows the user to append a new paragraph
node as a child of the current element, demonstrated in Fig.
10.2(d). This feature uses a similar procedure to the insertBefore
functionality. Lines 53–54 in function appendNode create a new node, line 55
inserts it as a child of the current node, and line 56 uses switchTo to update
currentNode and highlight the new node.
Replacing and Removing Elements Using replaceChild and removeChild
The next two table rows (lines 149–156) allow the user to replace the current
element with a new p element or simply remove the current element. Lines 150–152
contain a text field and a button that replaces the currently highlighted
element with a new paragraph node containing the text in the text field. This
feature is demonstrated in Fig.
The button in lines 151–152 calls function replaceCurrent, defined in lines
60–66. Lines 62–63 call createNewNode, in the same way as in insert and
appendNode, getting the text from the correct input field. Line 64 gets the
parent of currentNode, then calls the replaceChild method on the parent. The
replaceChild method works as follows:
parent.replaceChild( newChild, oldChild );
The parent's replaceChild method inserts newChild into its
list of children in place of old-Child.
The Remove Current feature, shown in Fig.
10.2(h), removes the current element entirely and highlights the parent. No
text field is required because a new element is not being created. The button in
lines 154-155 calls the remove function, defined in lines 69–79. If the node's
parent is the body element, line 72 alerts an errorthe program does not allow
the entire body element to be selected. Otherwise, lines 75–77 remove the
current element. Line 75 stores the old currentNode in variable oldNode. We do
this to maintain a reference to the node to be removed after we've changed the
value of currentNode. Line 76 calls switchTo to highlight the parent node.
Line 77 uses the removeChild method to remove the oldNode (a child of the new
currentNode) from its place in the XHTML document. In general,
parent.removeChild( child );
looks in parent's list of children for child and removes
The final button (lines 157–158) selects and highlights the parent element of
the currently highlighted element by calling the parent function, defined in
lines 82–90. Function parent simply gets the parent node (line 84), makes sure
it is not the body element, (line 86) and calls switchTo to highlight it (line
87). Line 89 alerts an error if the parent node is the body element. This
feature is shown in Fig.
This section introduced the basics of DOM tree traversal and manipulation.
Next, we introduce the concept of collections, which give you access to multiple
elements in a page.
10.4 DOM Collections
Included in the Document Object Model is the notion of collections
which are groups of related objects on a page. DOM collections are accessed as
properties of DOM objects such as the document object or a DOM node. The
document object has properties containing the images collection, links
collection, forms collection and anchors collection. These collections contain
all the elements of the corresponding type on the page. Figure
gives an example that uses the links collection to extract all of the
links on a page and display them together at the bottom of the page.
Using the links collection. (Part 1 of 2.)
Using the links collection. (Part 2 of 2.)
The XHTML body contains a paragraph (lines 46–59) with links at various
places in the text and an empty div (line 60) with id links. The body's onload
attribute specifies that the processlinks method is called when the body
Method processlinks declares variable linkslist (line 27) to store the
document's links collection, which is accessed as the links property of the
document object. Line 28 creates the string (contents) that will contain all the
document's links, to be inserted into the links div later. Line 31 begins a for
statement to iterate through each link. To find the number of elements in the
collection, we use the collection's length property.
Line 33 inside the for statement creates a variable (currentlink) that stores
the current link. Note that we can access the collection stored in linkslist
using indices in square brackets, just as we did with arrays. DOM collections
are stored in objects which have only one property and two methodsthe length
property, the item method and the namedItem method. The item methodan alternative to the square bracketed indicescan be used to access specific
elements in a collection by taking an index as an argument. The namedItem method
takes a name as a parameter and finds the element in the collection, if any,
whose id attribute or name attribute matches it.
Lines 34–36 add a span element to the contents string containing the current
link. Recall that the link method of a string object returns the string as a
link to the URL passed to the method. Line 35 uses the link method to create an
a (anchor) element containing the proper text and href attribute.
Notice that variable currentLink (a DOM node representing an a element) has a
specialized href property to refer to the link's href attribute. Many types of
XHTML elements are represented by special types of nodes that extend the
functionality of a basic DOM node. Line 39 inserts the contents into the empty
div with id "links" (line 60) in order to show all the links on the page in one
Collections allow easy access to all elements of a single type in a page.
This is useful for gathering elements into one place and for applying changes
across an entire page. For example, the forms collection could be used to
disable all form inputs after a submit button has been pressed to avoid multiple
submissions while the next page loads. The next section discusses how to