DOM Element Interface
The Element interface is the base for every element in a document, such as div or p. It provides the properties and methods shared by all elements, like classList, querySelector() and getAttribute().
Description
The Element interface is the most general base class from which all element objects in a Document inherit. It only has the methods and properties common to every kind of element. More specific interfaces build on it: HTMLElement for HTML elements, with subtypes such as HTMLInputElement, and SVGElement for SVG.
You usually get elements from the document with methods such as document.querySelector() or document.getElementById(), or create them with document.createElement(). With an element you can read and change its attributes, classes and content, find other elements inside it, and respond to events.
Element inherits from Node, which in turn inherits from EventTarget, so every element also has methods such as appendChild() and addEventListener().
Inheritance
Element inherits properties and methods from Node.
Properties
| Name | Description |
|---|---|
Element.children | The children property returns a live collection of an element's child elements, ignoring text and comments. |
Element.classList | The classList property gives a live list of an element's CSS classes, with methods to add, remove, toggle and check classes. |
Element.className | The className property gets or sets an element's class attribute as a single string. |
Element.firstElementChild | The firstElementChild property returns an element's first child element, skipping text nodes. |
Element.id | The id property gets or sets an element's id attribute. |
Element.innerHTML | The innerHTML property gets or sets the HTML markup inside an element. |
Element.nextElementSibling | The nextElementSibling property returns the element immediately after an element in its parent, skipping text. |
Element.outerHTML | The outerHTML property gets the HTML of an element including its own tag, or replaces the whole element when set. |
Element.tagName | The tagName property returns the name of an element's tag, in upper case for HTML elements. |
Methods
| Name | Description |
|---|---|
Element.append() | The append() method adds one or more nodes or text strings to the end of an element. |
Element.before() | The before() method inserts nodes or text just before an element, as siblings. |
Element.closest() | The closest() method searches up the tree from an element and returns the nearest ancestor, or the element itself, that matches a CSS selector. |
Element.getAttribute() | The getAttribute() method returns the value of an attribute on an element, or null if it isn't set. |
Element.getBoundingClientRect() | The getBoundingClientRect() method returns the size of an element and its position relative to the viewport. |
Element.hasAttribute() | The hasAttribute() method checks whether an element has a given attribute. |
Element.insertAdjacentHTML() | The insertAdjacentHTML() method parses an HTML string and inserts the result at a given position relative to an element, without replacing existing content. |
Element.matches() | The matches() method checks whether an element would be selected by a given CSS selector. |
Element.prepend() | The prepend() method adds one or more nodes or text strings at the start of an element, before its first child. |
Element.querySelector() | The querySelector() method returns the first element inside an element that matches a CSS selector, or null if nothing matches. |
Element.querySelectorAll() | The querySelectorAll() method returns a list of every element inside an element that matches a CSS selector. |
Element.remove() | The remove() method removes an element from the page. |
Element.removeAttribute() | The removeAttribute() method removes an attribute from an element. |
Element.replaceWith() | The replaceWith() method replaces an element in the page with other nodes or text. |
Element.scrollIntoView() | The scrollIntoView() method scrolls the page, and any scrollable containers, so that an element becomes visible. |
Element.setAttribute() | The setAttribute() method sets the value of an attribute on an element, adding the attribute if it doesn't exist. |
Element.toggleAttribute() | The toggleAttribute() method adds a boolean attribute if it's missing or removes it if it's present. |
Events
| Name | Description |
|---|---|
Element: click event | The click event fires when an element is activated by a mouse click, a tap, or the Enter or Space key on buttons and links. |
Element: keydown event | The keydown event fires when a key is pressed while an element (or something inside it) has focus, and repeats while the key is held. |
Example
Browser Support
The following information will show you the current browser support for the Element interface. Hover over a browser icon to see the version that first introduced support for this interface.
Desktop





Tablets & Mobile





