Element — a document element

Inherits from

Node, EventTarget


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 ...

Get JavaScript: The Definitive Guide, 6th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.