HTML and CSS Reference
In-Depth Information
Node Types, Names, and Values
As the createList() function loops through the child nodes of the source document, it
will need to be able to distinguish element nodes from text nodes as well as identify the
name of each element node it encounters. JavaScript provides the following properties to
determine each node's type, name, and value:
node .nodeType
node .nodeName
node .nodeValue
The nodeType property returns an integer indicating whether the node refers to an
element, a text string, a comment, an attribute, a document, or another type of node.
The nodeName property returns the name of the node within the document. Finally, the
nodeValue property returns the node's value. Figure 14-17 displays the values of these
three properties for the different types of nodes you'll typically encounter in a Web page
document.
Figure 14-17
node types, names, and values
Node
.nodeType
.nodeName
.nodeValue
Element
1
ELEMENT NAME
null
Attribute
2
attribute name
attribute value
Text
3
#text
text string
Comment
8
#comment
comment text
Document
9
#document
null
To see how these properties compare to a node tree, Figure 14-18 displays the
nodeType , nodeName , and nodeValue property values for each of the nodes shown
earlier in Figure 14-7.
Figure 14-18
node properties from the sample node tree
Node
.nodeType
.nodeName
.nodeValue
Document
9
#document
null
html
1
HTML
null
head
1
HEAD
null
body
1
BODY
null
title
1
TITLE
null
"Historic Documents"
3
#text
Historic Documents
h1
1
H1
null
"Dept. of History"
3
#text
Dept. of History
p
1
P
null
"The "
3
#text
The
b
1
B
null
"constitution"
3
#text
constitution
" online"
3
#text
online
Search WWH ::




Custom Search