Java Reference
In-Depth Information
Getting Elements
The DOM API provides several methods that allow us to access any element on a page.
These methods will return a node object or a node list, which is an array-like object. These
objects can then be assigned to a variable and be inspected or modified.
For example, we can access the body element of a web page and assign it to the variable
body using the following code:
body = document.body;
<< <body>
Now that we have a reference to the body element, we can check its type:
typeof body;
<< "object";
This is a special Node object with a number of properties and methods that we can use to
find information about, or modify, the body element.
For example, we can use the nodeType property to find out what type of node it is:
body.nodeType;
<< 1
All nodes have a numerical code to signify what type they are. These are summarised in the
table below.
Code Type
1
element
2
attribute
3
text
8
comment
9
document
Search WWH ::




Custom Search