Java Reference
In-Depth Information
The variable
container
now contains the root element, which is
<html/>
. The
documentElement
property returned a reference to this element in the form of an object, an
Element
object to be precise. The
Element
object has its own set of properties and methods. If
you want to use them, you can refer to them by using the variable name, followed by the method
or property name:
container.elementObjectProperty
Fortunately, the
Element
object has only one property.
the element Object
The
Element
object is quite simple, especially compared to the
Node
object (which you are
introduced to later). It exposes only a handful of
members
(properties and methods).
member name
desCription
Gets the element's tag name
tagName
Gets the value of an attribute
getAttribute()
Sets an attribute with a specified value
setAttribute()
Removes a specific attribute and its value from the element
removeAttribute()
Getting the Element's Tag Name: The tagName Property
The sole property of the
Element
object is a reference to the tag name of the element: the
tagName
property.
In the previous example, the variable
container
contained the
<html/>
element. Add the following
highlighted line, which makes use of the
tagName
property:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<h1 id="heading1">My Heading</h1>
<p id="paragraph1">This is some text in a paragraph</p>
<script>
var container = document.documentElement;
alert(container.tagName);
</script>
</body>
</html>
This code will now return proof that your variable
container
holds the outermost element, and by
implication all other elements within it (see Figure 9-5).