Skip to Main Content
Head Rush Ajax
book

Head Rush Ajax

by Brett McLaughlin
March 2006
Beginner to intermediate content levelBeginner to intermediate
448 pages
13h 33m
English
O'Reilly Media, Inc.
Content preview from Head Rush Ajax
236
Chapter 4
if (someNode.nodeType == Node.ELEMENT_NODE) {
// Do something with the element node
} else if (someNode.nodeType == Node.TEXT_NODE) {
// Do something with the text node
}
I like most of this DOM stuff so far, but all
this null value and undened stuff kind of
freaks me out. Can’t I just ask the node if it’s
an element, or text, or whatever?
Every node has a property called nodeType,
along with the nodeName and nodeValue
proeprties that you’ve already seen. The
nodeType property returns a number, and that
number maps to a value stored in the Node class.
There’s a value for each type of node, so you can
use these values to gure out exactly what kind
of node you’re working with, like this:
You can! (well, sort of)
Here’s the nodeType property.
It returns a number...
...that you can compare to the
numbers dened in the Node class.
There are numbers dened
for all the node types,
including elements, text,
and attributes.
Once you know what type
of node you have, you can
avoid using properties that
would return null values.
node types
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick
Head First Ajax

Head First Ajax

Rebecca M. Riordan

Publisher Resources

ISBN: 0596102259Errata PageSupplemental Content