Element |
an HTML or XML element |
Availability
DOM Level
1 Core
Inherits from/Overrides
Node Element
Subinterfaces
HTMLElement
Properties
- readonly String tagName
-
The tag
name of the element. This is the string "P" for an HTML
<p> element, for example. For HTML
documents, the tag name is returned in uppercase, regardless of its
capitalization in the document source. XML documents are
case-sensitive, and the tag name is returned exactly as it is written
in the document source. This property has the same value as the
nodeName property of the Node interface.
Methods
- getAttribute( )
-
Returns the value of a named attribute as a string.
- getAttributeNS( ) [DOM Level 2]
-
Returns the string value of an attribute specified by local name and
namespace URI. Useful only with XML documents that use namespaces.
- getAttributeNode( )
-
Returns the value of a named attribute as an Attr node.
- getAttributeNodeNS( ) [DOM Level 2]
-
Returns the Attr value of an attribute specified by local name and
namespace URI. Useful only with XML documents that use namespaces.
- getElementsByTagName( )
-
Returns an array (technically, a NodeList) of all descendant Element
nodes of this element that have the specified tag name, in the order
in which they appear in the document.
- getElementsByTagNameNS( ) [DOM Level 2]
-
Like getElementsByTagName( ), except that the
element tag name is specified by local name and namespace URI. Useful
only with XML documents that use namespaces.
- hasAttribute( ) [DOM Level 2]
-
Returns true if this element has an attribute with
the specified name, or false otherwise. Note that
this method returns true if the named attribute is
explicitly specified in the document source or if the
document's DTD specifies a default value for the named
attribute.
- hasAttributeNS( ) [DOM Level 2]
-
Like hasAttribute( ), except that the attribute is
specified by a combination of local name and namespace URI. This
method is useful only with XML documents that use namespaces.
- removeAttribute( )
-
Deletes the named attribute from this element. Note, however, that
this method deletes only attributes that are explicitly specified in
the document source for this element. If the DTD specifies a default
value for this attribute, that default becomes the new value of the
attribute.
- removeAttributeNS( ) [DOM Level 2]
-
Like removeAttribute( ), except that the attribute
to be removed is specified by a combination of local name and
namespace URI. Useful only for XML documents that use namespaces.
- removeAttributeNode( )
-
Removes the specified Attr node from the list of attributes for this
element. Note that this works only to remove attributes that are
explicitly specified in the document source for this attribute. If
the DTD specifies a default value for the removed attribute, a new
Attr node is created to represent the default value of the attribute.
- setAttribute( )
-
Sets the named attribute to the specified string value. If an
attribute with that name does not already exist, a new attribute is
added to the element.
- setAttributeNS( ) [DOM Level 2]
-
Like setAttribute( ), except that the attribute to
be set is specified by the combination of a local name and a
namespace URI. Useful only with XML documents that use namespaces.
- setAttributeNode( )
-
Adds the specified Attr node to the list of attributes for this
element. If an attribute with the same name already exists, its value
is replaced.
- setAttributeNodeNS( ) [DOM Level 2]
-
Like setAttributeNode( ), but this method is
suitable for use with nodes returned by
Document.createAttributeNS( ). Useful only with
XML documents that use namespaces.
Description
The Element interface represents HTML or XML elements or tags. The
tagName property specifies the name of the
element. The getElementsByTagName( ) method
provides a powerful way to locate element descendants of a given
element that have a specified tag name. The various other methods of
this interface provide access to the attributes of the element. If
you give an element a unique identifier using the
id attribute in your document source, you can then
easily locate the Element node that represents that document element
with the useful Document.getElementById( ) method.
To create a new Element node for insertion into a document, use
Document.createElement( ).
In HTML documents (and many XML documents) all attributes have simple
string values, and you can use the simple methods
getAttribute( ) and setAttribute(
) for any attribute manipulation you need to do.
If you are working with XML documents that may contain entity
references as part of attribute values, you will have to work with
Attr objects and their subtree of nodes. You can get and set the Attr
object for an attribute with getAttributeNode( )
and setAttributeNode( ), or you can iterate
through the Attr nodes in the attributes[] array
of the Node interface. If you are working with an XML document that
uses XML namespaces, you'll need to use the various methods
whose names end with "NS".
In the DOM Level 1 specification, the normalize( )
method was part of the Element interface. In the Level 2
specification, normalize( ) is instead part of the
Node interface. All Element nodes inherit this method and can still
use it.
See Also
HTMLElement, Node
Type of
Attr.ownerElement, Document.documentElement
Passed to
Document.getOverrideStyle( ), AbstractView.getComputedStyle( )
Returned by
Document.createElement( ), Document.createElementNS( ),
Document.getElementById( )
|