Element is the most general base class from which all objects in a Document inherit. It only has methods and properties common to all kinds of element. More specific classes inherit from Element. For example, the HTMLElement interface is the base interface for HTML elements, while the SVGElement interface is the basis for all SVG elements. Most functionality is specified further down the class hierarchy.
Languages outside the realm of the Web platform, like XUL through the XULElement interface, also implement Element.
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 11.666666666666666%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 70" preserveAspectRatio="xMinYMin meet"><a xlink:href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25 121,20 121,30 111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/en-US/docs/Web/API/Node" target="_top"><rect x="151" y="1" width="75" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="188.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Node</text></a><polyline points="226,25 236,20 236,30 226,25" stroke="#D4DDE4" fill="none"/><line x1="236" y1="25" x2="266" y2="25" stroke="#D4DDE4"/><a xlink:href="https://developer.mozilla.org/en-US/docs/Web/API/Element" target="_top"><rect x="266" y="1" width="75" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="303.5" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">Element</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
Properties
Inherits properties from its parent interface, Node, and by extension that interface's parent, EventTarget. It implements the properties of ParentNode, ChildNode, NonDocumentTypeChildNode, and Animatable.
Element.assignedSlotRead only- Returns the
HTMLSlotElementinterface associated with the element. Element.attributesRead only- Returns a
NamedNodeMapobject containing the assigned attributes of the corresponding HTML element. Element.classListRead only- Returns a
DOMTokenListcontaining the list of class attributes. Element.className- Is a
DOMStringrepresenting the class of the element. Element.clientHeightRead only- Returns a
Numberrepresenting the inner height of the element. Element.clientLeftRead only- Returns a
Numberrepresenting the width of the left border of the element. Element.clientTopRead only- Returns a
Numberrepresenting the width of the top border of the element. Element.clientWidthRead only- Returns a
Numberrepresenting the inner width of the element. Element.computedNameRead only- Returns a
DOMStringcontaining the label exposed to accessibility. Element.computedRoleRead only- Returns a
DOMStringcontaining the ARIA role that has been applied to a particular element. Element.id- Is a
DOMStringrepresenting the id of the element. Element.innerHTML- Is a
DOMStringrepresenting the markup of the element's content. Element.localNameRead only- A
DOMStringrepresenting the local part of the qualified name of the element. Element.namespaceURIRead only- The namespace URI of the element, or
nullif it is no namespace.Note: In Firefox 3.5 and earlier, HTML elements are in no namespace. In later versions, HTML elements are in the
http://www.w3.org/1999/xhtmlnamespace in both HTML and XML trees. NonDocumentTypeChildNode.nextElementSiblingRead only- Is a
Element, the element immediately following the given one in the tree, ornullif there's no sibling node. Element.outerHTML- Is a
DOMStringrepresenting the markup of the element including its content. When used as a setter, replaces the element with nodes parsed from the given string. Element.prefixRead only- A
DOMStringrepresenting the namespace prefix of the element, ornullif no prefix is specified. NonDocumentTypeChildNode.previousElementSiblingRead only- Is a
Element, the element immediately preceding the given one in the tree, ornullif there is no sibling element. Element.scrollHeightRead only- Returns a
Numberrepresenting the scroll view height of an element. Element.scrollLeft- Is a
Numberrepresenting the left scroll offset of the element. Element.scrollLeftMaxRead only- Returns a
Numberrepresenting the maximum left scroll offset possible for the element. Element.scrollTop- Is a
Numberrepresenting the top scroll offset the element. Element.scrollTopMaxRead only- Returns a
Numberrepresenting the maximum top scroll offset possible for the element. Element.scrollWidthRead only- Returns a
Numberrepresenting the scroll view width of the element. Element.shadowRootRead only- Returns the youngest shadow root that is hosted by the element.
Element.slot- Returns the name of the shadow DOM slot attached to the element. A slot is a placeholder inside a web component that users can fill with their own markup.
Element.tabStop- Is a
Booleanindicating if the element can receive input focus via the tab key. Element.tagNameRead only- Returns a
Stringwith the name of the tag for the given element. Element.undoManagerRead only- Returns the
UndoManagerassociated with the element. Element.undoScope- Is a
Booleanindicating if the element is an undo scope host, or not.
Note: DOM Level 3 defined namespaceURI, localName and prefix on the Node interface. In DOM4 they were moved to Element.
This change is implemented in Chrome since version 46.0 and Firefox since version 48.0.
Event handlers
Element.ongotpointercapture- Returns the event handler for the
gotpointercaptureevent type. Element.onlostpointercapture- Returns the event handler for the
lostpointercaptureevent type.
Obsolete event handlers
Element.onwheel- Returns the event handling code for the
wheelevent. This is now implemented onGlobalEventHandlers.
Methods
Inherits methods from its parents Node, and its own parent, EventTarget, and implements those of ParentNode, ChildNode, NonDocumentTypeChildNode, and Animatable.
EventTarget.addEventListener()- Registers an event handler to a specific event type on the element.
Element.attachShadow()- Attatches a shadow DOM tree to the specified element and returns a reference to its
ShadowRoot. Element.animate()- A shortcut method to create and run an animation on an element. Returns the created Animation object instance.
Element.closest()- Returns the
Elementwhich is the closest ancestor of the current element (or the current element itself) which matches the selectors given in parameter. Element.createShadowRoot()- Creates a shadow DOM on on the element, turning it into a shadow host. Returns a
ShadowRoot. EventTarget.dispatchEvent()- Dispatches an event to this node in the DOM and returns a
Booleanthat indicates that at least one handler has not canceled it. Element.find()- ...
Element.findAll()- ...
Element.getAnimations()- Returns an array of Animation objects currently active on the element.
Element.getAttribute()- Retrieves the value of the named attribute from the current node and returns it as an
Object. Element.getAttributeNames()- Returns an array of attribute names from the current element.
Element.getAttributeNS()- Retrieves the value of the attribute with the specified name and namespace, from the current node and returns it as an
Object. Element.getAttributeNode()- Retrieves the node representation of the named attribute from the current node and returns it as an
Attr. Element.getAttributeNodeNS()- Retrieves the node representation of the attribute with the specified name and namespace, from the current node and returns it as an
Attr. Element.getBoundingClientRect()- ...
Element.getClientRects()- Returns a collection of rectangles that indicate the bounding rectangles for each line of text in a client.
Element.getElementsByClassName()- Returns a live
HTMLCollectionthat contains all descendants of the current element that possess the list of classes given in the parameter. Element.getElementsByTagName()- Returns a live
HTMLCollectioncontaining all descendant elements, of a particular tag name, from the current element. Element.getElementsByTagNameNS()- Returns a live
HTMLCollectioncontaining all descendant elements, of a particular tag name and namespace, from the current element. Element.hasAttribute()- Returns a
Booleanindicating if the element has the specified attribute or not. Element.hasAttributeNS()- Returns a
Booleanindicating if the element has the specified attribute, in the specified namespace, or not. Element.hasAttributes()- Returns a
Booleanindicating if the element has one or more HTML attributes present. Element.insertAdjacentElement- Inserts a given element node at a given position relative to the element it is invoked upon.
Element.insertAdjacentHTML- Parses the text as HTML or XML and inserts the resulting nodes into the tree in the position given.
Element.insertAdjacentText- Inserts a given text node at a given position relative to the element it is invoked upon.
Element.matches()- Returns a
Booleanindicating whether or not the element would be selected by the specified selector string. Element.querySelector()- Returns the first
Nodewhich matches the specified selector string relative to the element. Element.querySelectorAll- Returns a
NodeListof nodes which match the specified selector string relative to the element. Element.releasePointerCapture- Releases (stops) pointer capture that was previously set for a specific
pointer event. ChildNode.remove()- Removes the element from the children list of its parent.
Element.removeAttribute()- Removes the named attribute from the current node.
Element.removeAttributeNS()- Removes the attribute with the specified name and namespace, from the current node.
Element.removeAttributeNode()- Removes the node representation of the named attribute from the current node.
EventTarget.removeEventListener()- Removes an event listener from the element.
Element.requestFullscreen()- Asynchronously asks the browser to make the element full-screen.
Element.requestPointerLock()- Allows to asynchronously ask for the pointer to be locked on the given element.
Element.scrollIntoView()- Scrolls the page until the element gets into the view.
Element.setAttribute()- Sets the value of a named attribute of the current node.
Element.setAttributeNS()- Sets the value of the attribute with the specified name and namespace, from the current node.
Element.setAttributeNode()- Sets the node representation of the named attribute from the current node.
Element.setAttributeNodeNS()- Setw the node representation of the attribute with the specified name and namespace, from the current node.
Element.setCapture()- Sets up mouse event capture, redirecting all mouse events to this element.
Element.setPointerCapture()- Designates a specific element as the capture target of future
pointer events.
Specifications
| Specification | Status | Comment |
|---|---|---|
| Web Animations | Working Draft | Added the getAnimations() method. |
| UndoManager and DOMTransaction The definition of 'Element' in that specification. |
Editor's Draft | Added the undoScope and undoManager properties. |
| Pointer Events – Level 2 The definition of 'Element' in that specification. |
Editor's Draft | Added the following event handlers: ongotpointercapture and onlostpointercapture.Added the following methods: setPointerCapture() and releasePointerCapture(). |
| Pointer Events The definition of 'Element' in that specification. |
Recommendation | Added the following event handlers: ongotpointercapture and onlostpointercapture.Added the following methods: setPointerCapture() and releasePointerCapture(). |
| Selectors API Level 2 The definition of 'Element' in that specification. |
Obsolete | Added the following methods: matches() (implemented as mozMatchesSelector()), find(), findAll(). |
| Selectors API Level 1 The definition of 'Element' in that specification. |
Obsolete | Added the following methods: querySelector() and querySelectorAll(). |
| Pointer Lock The definition of 'Element' in that specification. |
Candidate Recommendation | Added the requestPointerLock() method. |
| Fullscreen API The definition of 'Element' in that specification. |
Living Standard | Added the requestFullscreen() method. |
| DOM Parsing and Serialization The definition of 'Element' in that specification. |
Working Draft | Added the following properties: innerHTML, and outerHTML.Added the following method: insertAdjacentHTML(). |
| CSS Object Model (CSSOM) View Module The definition of 'Element' in that specification. |
Working Draft | Added the following properties: scrollTop, scrollLeft, scrollWidth, scrollHeight, clientTop, clientLeft, clientWidth, and clientHeight.Added the following methods: getClientRects(), getBoundingClientRect(), and scrollIntoView(). |
| Element Traversal Specification The definition of 'Element' in that specification. |
Obsolete | Added inheritance of the ElementTraversal interface. |
| DOM The definition of 'Element' in that specification. |
Living Standard | Removed the following methods: closest(), setIdAttribute(), setIdAttributeNS(), and setIdAttributeNode().Removed the schemaTypeInfo property.Modified the return value of getElementsByTag() and getElementsByTagNS().Moved hasAttributes() from the Node interface to this one.Inserted insertAdjacentElement() and insertAdjacentText(). |
| Document Object Model (DOM) Level 3 Core Specification The definition of 'Element' in that specification. |
Obsolete | Added the following methods: setIdAttribute(), setIdAttributeNS(), and setIdAttributeNode(). These methods were never implemented and have been removed in later specifications.Added the schemaTypeInfo property. This property was never implemented and has been removed in later specifications. |
| Document Object Model (DOM) Level 2 Core Specification The definition of 'Element' in that specification. |
Obsolete | The normalize() method has been moved to Node. |
| Document Object Model (DOM) Level 1 Specification The definition of 'Element' in that specification. |
Obsolete | Initial definition. |
Browser compatibility
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| Basic support | 1.0 | (Yes) | 1.0 (1.7 or earlier) | (Yes) | (Yes) | 1.0 |
children |
(Yes) | (Yes) | 3.0 (1.9) | 7.0 with a significant bug [1] 9.0 according to the spec |
(Yes) | No support |
childElementCount, nextElementSibling, previousElementSibling |
(Yes) | (Yes) | 3.5 (1.9.1) | 9.0 | (Yes) | (Yes) |
firstElementChild, lastElementChild |
(Yes) | (Yes) | 3.0 (1.9) | 9.0 | (Yes) | (Yes) |
classList |
(Yes) | (Yes) | 3.6 (1.9.2) | (Yes) | (Yes) | |
outerHTML |
(Yes) | (Yes) | 11 (11) | (Yes) | (Yes) | (Yes) |
clientLeft, clientTop |
(Yes) | (Yes) | 3.5 (1.9.1) | (Yes) | (Yes) | (Yes) |
getBoundingClientRect(), getClientRects() |
(Yes) | (Yes) | 3.0 (1.9) | (Yes) | (Yes) | (Yes) |
querySelector(), querySelectorAll() |
1.0 | (Yes) | 3.5 (1.9.1) | 8.0 | 10.0 | 3.2 (525.3) |
insertAdjacentHTML() |
1.0 | (Yes) | 8 (8) | 4.0 | 7.0 | 4.0 (527) |
setCapture() |
No support | No support | 4.0 (2) | No support | No support | No support |
oncopy, oncut, onpaste |
No support | No support | 3.0 (1.9) | (Yes) | No support | |
onwheel |
No support | (Yes) | 17 (17) | No support | No support | No support |
ongotpointercapture, onlostpointercapture, setPointerCapture(), and releasePointerCapture() |
52.0 [4] | (Yes) | (Yes) [3] | 10.0 | No support | No support |
matches() |
(Yes) with the non-standard name webkitMatchesSelector |
(Yes) webkit ms | 3.6 (1.9.2) with the non-standard name mozMatchesSelector34 (34) with the standard name |
9.0 with the non-standard name msMatchesSelector |
11.5 with the non-standard name oMatchesSelector15.0 with the non-standard name webkitMatchesSelector |
5.0 with the non-standard name webkitMatchesSelector |
find() and findAll() |
No support | No support | No support | No support | No support | No support |
requestPointerLock() |
16.0 webkit, behind an about:flags 22.0 webkit (with special cases, progressively lifted see [2]) |
(Yes) | 14 (14)moz | No support | No support | No support |
requestFullscreen() |
14.0 webkit | (Yes) | 10 (10) moz | 11.0 ms | 12.10 15.0 webkit |
5.1 webkit |
undoManager and undoScope |
No support | No support | (Yes) (behind the dom.undo_manager.enabled pref) |
No support | No support | No support |
attributes |
? | No support | 22 (22) Before this it was available via the Node interface that any element inherits. |
? | ? | ? |
scrollTopMax() and scrollLeftMax() |
No support | No support | 16 (16) | No support | No support | No support |
closest() |
? | (Yes) | 35 (35) | ? | ? | ? |
hasAttributes() |
(Yes) | No support | 1.0 (1.7 or earlier) (on the Node interface)35 (35) (on this interface |
(Yes) | (Yes) | (Yes) |
insertAdjacentElement(), insertAdjacentText() |
(Yes) | No support | 48.0 (48.0) | ? | (Yes) | (Yes) |
assignedSlot, attatchShadow, shadowRoot, and slot |
53 | No support | ? | ? | ? | ? |
computedRole and computedName |
41[4] | ? | ? | ? | 28[4] | ? |
| Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Basic support | 1.0 | (Yes) | 1.0 (1) | (Yes) | (Yes) | 1.0 | ||
scrollTopMax() and scrollLeftMax() |
No support | No support | 16.0 (16) | No support | No support | No support | ||
closest() |
? | (Yes) | 35.0 (35) | ? | ? | ? | ||
hasAttributes() |
(Yes) | No support | 1.0 (1.0) (on the Node interface)35.0 (35) (on this interface |
(Yes) | (Yes) | (Yes) | ||
insertAdjacentElement(), insertAdjacentText() |
(Yes) | (Yes) | 48.0 (48.0) | ? | (Yes) | (Yes) | ||
assignedSlot, attatchShadow, shadowRoot, and slot |
No support | 53.0 | No support | ? | ? | ? | ? | 53 |
computedRole and computedName |
No support | No support | ? | ? | ? | 28[4] | ? | No support |
[1] Internet Explorer 7 and 8 incorrectly return the comments as part of the children of an Element. This is fixed in Internet Explorer 9 and later.
[2] Chrome 16 allowed webkitRequestPointerLock() only in fullscreen; Chrome 21 for trusted web site (permission asked); Chrome 22 allowed it by default for all same-origin document; Chrome 23 allowed it in sandboxed <iframe> if the non-standard value webkit-allow-pointer-lock is set to the sandbox attribute.
[3] Implementation withdrawn. See bug 1166347.
[4] Behind a flag.