Document Object Model (DOM): Objects and Collections
Traversing and Modifying a DOM Tree
The DOM gives you access to the elements of a document,
allowing you to modify the contents of a page dynamically
using event-driven JavaScript. This section introduces
properties and methods of all DOM nodes that enable you to
traverse the DOM tree, modify nodes and create or delete
content dynamically.
Figure 10.2 shows some of the functionality of DOM nodes,
as well as two additional methods of the document object.
The program allows you to highlight, modify, insert and
remove elements.
Lines 117–132 contain basic XHTML elements and content.
Each element has an id attribute, which is also displayed at
the beginning of the element in square brackets. For
example, the id of the h1 element in lines 117–118 is set to
bigheading, and the heading text begins with [bigheading].
This allows the user to see the id of each element in the
page. The body also contains an h3 heading, several p
elements, and an unordered list.
A div element (lines 133–162) contains the remainder of
the XHTML body. Line 134 begins a form element, assigning
the empty string to the required action attribute (because
we’re not submitting to a server) and returning false to the
onsubmit attribute. When a form’s onsubmit handler returns
false, the navigation to the address specified in the action
attribute is aborted. This allows us to modify the page
using JavaScript event handlers without reloading the
original, unmodified XHTML.
Click here for larger image
Fig. 10.2 - Basic DOM functionality.
Click here for larger image
Fig. 10.2a - a)This is the page when it first loads. It
begins with the large heading highlighted. (Part 1 of 8.)
Click here for larger image
Fig. 10.2b - b) This is the document after using the Get By
id button to select para3. (Part 2 of 8.)
Click here for larger image
Fig. 10.2c - c) This is the document after inserting a new
paragraph before the selected one. (Part 3 of 8.)
Click here for larger image
Fig. 10.2d - d) Using the Append Child button, a child
paragraph is created. (Part 4 of 8.)
Click here for larger image
Fig. 10.2e - e) The selected paragraph is replaced with a
new one. (Part 5 of 8.)
Click here for larger image
Fig. 10.2f - f) The Get Parent button gets the parent of the
selected node. (Part 6 of 8.)
Click here for larger image
Fig. 10.2g - g) Now we select the first list item. (Part 7 of 8.)
Click here for larger image
Fig. 10.2h - h) The Remove Current button removes the
current node and selects its parent. (Part 8 of 8.)
A table (lines 135–160) contains the controls for
modifying and manipulating the elements on the page. Each of
the six buttons calls its own event-handling function to
perform the action described by its value.
The JavaScript code begins by declaring two variables.
The variable currentNode(line 27) keeps track of the
currently highlighted node, because the functionality of the
buttons depends on which node is currently selected. The
body’s onload attribute (line 116) initializes currentNode
to the h1 element with id bigheading. Variable idcount (line
28) is used to assign a unique id to any new elements that
are created. The remainder of the Java-Script code contains
event handling functions for the XHTML buttons and two
helper functions that are called by the event handlers. We
now discuss each button and its corresponding event handler
in detail.
Document Object Model (DOM): Objects and Collections
Document Object Model (DOM): Objects and Collections
Finding and Highlighting an Element Using getElementById and className
|