you’re on your way �
233
the document object model
Sweet! Now I can nd any element I want, move up
and down the DOM tree... I’ll bet I can get element
names and text values, too, right?
Remember, everything in a DOM tree is a node.
Elements and text are special kinds of nodes, but
they’re still nodes. Anytime you have a node, you
can get the name of the node with nodeName,
and the value of the node with nodeValue.
You’ve got to be careful what type of node you’re
working on, though, or you could end up getting
a null value when you’re expecting the name
of an element or a string of text. An element
node has a name, like “div” or “img”, but won’t
have a value. And a text node won’t have a
name, but it will have a value: the actual text
stored in the node.
Let’s take a look and see how this works:
The node knows... pretty much everything.
“breadth-rst”
em
div
nodeName
nodeValue
“div”
null/undened
“breadth-rst”
The nodeValue for an
element node is undened.
The nodeValue
of a text node
is its text.
Element nodes all
have a nodeName.
Text nodes do not
have a nodeName.
node type
node
element
node
“em”
null/undened
element
node
null/undenedtext node