Cover by David Flanagan

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Name

Element — a document element

Inherits from

Node, EventTarget

Synopsis

An Element object represents an element in an HTML or XML document. The tagName property specifies the tag name or type of the element. Standard HTML attributes of the element are available through JavaScript properties of the Element object. Attributes, including XML attributes and nonstandard HTML attributes can also be accessed with the get Attribute() and setAttribute() methods. Element content is available through properties inherited from Node. If you are only interested in the Element relatives of an Element, you can use the children property or firstElementChild, nextElementSibling, and related properties.

There are a number of ways to obtain Element objects from documents. The document Element property of a Document refers to the root element for that document, such as the <html> element of an HTML document. For HTML documents, the head and body properties are similar: they refer to the <head> and <body> elements of the document. To locate a specific named element by its unique id attribute, use Document.getElementById(). As described in Selecting Document Elements, you can also obtain Element objects with Document and Element methods such as getElementsByTagName(), getElementsByClassName(), and querySelectorAll(). Finally, you can create new Element objects for insertion into a document with Document.createElement().

Web browsers fire many different kinds of events on document elements, and Element objects ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required