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

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.