Element
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Element is the most general base class from which all element objects (i.e., objects that represent elements) in aDocument inherit. It only has methods and properties common to all kinds of elements. More specific classes inherit fromElement.
For example, theHTMLElement interface is the base interface for HTML elements. Similarly, theSVGElement interface is the basis for all SVG elements, and theMathMLElement interface is the base interface for MathML elements. Most functionality is specified further down the class hierarchy.
Languages outside the realm of the Web platform, like XUL through theXULElement interface, also implementElement.
In this article
Instance properties
Element inherits properties from its parent interface,Node, and by extension that interface's parent,EventTarget.
Element.assignedSlotRead onlyReturns a
HTMLSlotElementrepresenting the<slot>the node is inserted in.Element.attributesRead onlyReturns a
NamedNodeMapobject containing the assigned attributes of the corresponding HTML element.Element.childElementCountRead onlyReturns the number of child elements of this element.
Element.childrenRead onlyReturns the child elements of this element.
Element.classListRead onlyReturns a
DOMTokenListcontaining the list of class attributes.Element.classNameA string representing the class of the element.
Element.clientHeightRead onlyReturns a number representing the inner height of the element.
Element.clientLeftRead onlyReturns a number representing the width of the left border of the element.
Element.clientTopRead onlyReturns a number representing the width of the top border of the element.
Element.clientWidthRead onlyReturns a number representing the inner width of the element.
Element.currentCSSZoomRead onlyA number indicating the effective zoom size of the element, or 1.0 if the element is not rendered.
Element.elementTimingExperimentalA string reflecting the
elementtimingattribute which marks an element for observation in thePerformanceElementTimingAPI.Element.firstElementChildRead onlyReturns the first child element of this element.
Element.idA string representing the id of the element.
Element.innerHTMLA string representing the markup of the element's content.
Element.lastElementChildRead onlyReturns the last child element of this element.
Element.localNameRead onlyA string representing the local part of the qualified name of the element.
Element.namespaceURIRead onlyThe namespace URI of the element, or
nullif it is no namespace.Element.nextElementSiblingRead onlyAn
Element, the element immediately following the given one in the tree, ornullif there's no sibling node.Element.outerHTMLA string representing the markup of the element including its content. When used as a setter, replaces the element with nodes parsed from the given string.
Element.partRepresents the part identifier(s) of the element (i.e., set using the
partattribute), returned as aDOMTokenList.Element.prefixRead onlyA string representing the namespace prefix of the element, or
nullif no prefix is specified.Element.previousElementSiblingRead onlyAn
Element, the element immediately preceding the given one in the tree, ornullif there is no sibling element.Element.scrollHeightRead onlyReturns a number representing the scroll view height of an element.
Element.scrollLeftA number representing the left scroll offset of the element.
Element.scrollLeftMaxNon-standardRead onlyReturns a number representing the maximum left scroll offset possible for the element.
Element.scrollTopA number representing number of pixels the top of the element is scrolled vertically.
Element.scrollTopMaxNon-standardRead onlyReturns a number representing the maximum top scroll offset possible for the element.
Element.scrollWidthRead onlyReturns a number representing the scroll view width of the element.
Element.shadowRootRead onlyReturns the open shadow root that is hosted by the element, or null if no open shadow root is present.
Element.slotReturns the name of the shadow DOM slot the element is inserted in.
Element.tagNameRead onlyReturns a string with the name of the tag for the given element.
Instance properties included from ARIA
TheElement interface also includes the following properties.
Element.ariaAtomicA string reflecting the
aria-atomicattribute, which indicates whether assistive technologies will present all, or only parts of, the changed region based on the change notifications defined by thearia-relevantattribute.Element.ariaAutoCompleteA string reflecting the
aria-autocompleteattribute, which indicates whether inputting text could trigger display of one or more predictions of the user's intended value for a combobox, searchbox, or textbox and specifies how predictions would be presented if they were made.Element.ariaBrailleLabelA string reflecting the
aria-braillelabelattribute, which defines the braille label of the element.Element.ariaBrailleRoleDescriptionA string reflecting the
aria-brailleroledescriptionattribute, which defines the ARIA braille role description of the element.Element.ariaBusyA string reflecting the
aria-busyattribute, which indicates whether an element is being modified, as assistive technologies may want to wait until the modifications are complete before exposing them to the user.Element.ariaCheckedA string reflecting the
aria-checkedattribute, which indicates the current "checked" state of checkboxes, radio buttons, and other widgets that have a checked state.Element.ariaColCountA string reflecting the
aria-colcountattribute, which defines the number of columns in a table, grid, or treegrid.Element.ariaColIndexA string reflecting the
aria-colindexattribute, which defines an element's column index or position with respect to the total number of columns within a table, grid, or treegrid.Element.ariaColIndexTextA string reflecting the
aria-colindextextattribute, which defines a human readable text alternative of aria-colindex.Element.ariaColSpanA string reflecting the
aria-colspanattribute, which defines the number of columns spanned by a cell or gridcell within a table, grid, or treegrid.Element.ariaCurrentA string reflecting the
aria-currentattribute, which indicates the element that represents the current item within a container or set of related elements.Element.ariaDescriptionA string reflecting the
aria-descriptionattribute, which defines a string value that describes or annotates the current element.Element.ariaDisabledA string reflecting the
aria-disabledattribute, which indicates that the element is perceivable but disabled, so it is not editable or otherwise operable.Element.ariaExpandedA string reflecting the
aria-expandedattribute, which indicates whether a grouping element owned or controlled by this element is expanded or collapsed.Element.ariaHasPopupA string reflecting the
aria-haspopupattribute, which indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.Element.ariaHiddenA string reflecting the
aria-hiddenattribute, which indicates whether the element is exposed to an accessibility API.Element.ariaInvalidA string reflecting the
aria-invalidattribute, which indicates the entered value does not conform to the format expected by the application.Element.ariaKeyShortcutsA string reflecting the
aria-keyshortcutsattribute, which indicates keyboard shortcuts that an author has implemented to activate or give focus to an element.Element.ariaLabelA string reflecting the
aria-labelattribute, which defines a string value that labels the current element.Element.ariaLevelA string reflecting the
aria-levelattribute, which defines the hierarchical level of an element within a structure.Element.ariaLiveA string reflecting the
aria-liveattribute, which indicates that an element will be updated, and describes the types of updates the user agents, assistive technologies, and user can expect from the live region.Element.ariaModalA string reflecting the
aria-modalattribute, which indicates whether an element is modal when displayed.Element.ariaMultilineA string reflecting the
aria-multilineattribute, which indicates whether a text box accepts multiple lines of input or only a single line.Element.ariaMultiSelectableA string reflecting the
aria-multiselectableattribute, which indicates that the user may select more than one item from the current selectable descendants.Element.ariaOrientationA string reflecting the
aria-orientationattribute, which indicates whether the element's orientation is horizontal, vertical, or unknown/ambiguous.Element.ariaPlaceholderA string reflecting the
aria-placeholderattribute, which defines a short hint intended to aid the user with data entry when the control has no value.Element.ariaPosInSetA string reflecting the
aria-posinsetattribute, which defines an element's number or position in the current set of listitems or treeitems.Element.ariaPressedA string reflecting the
aria-pressedattribute, which indicates the current "pressed" state of toggle buttons.Element.ariaReadOnlyA string reflecting the
aria-readonlyattribute, which indicates that the element is not editable, but is otherwise operable.Element.ariaRelevantNon-standardA string reflecting the
aria-relevantattribute, which indicates what notifications the user agent will trigger when the accessibility tree within a live region is modified. This is used to describe what changes in anaria-liveregion are relevant and should be announced.Element.ariaRequiredA string reflecting the
aria-requiredattribute, which indicates that user input is required on the element before a form may be submitted.Element.ariaRoleDescriptionA string reflecting the
aria-roledescriptionattribute, which defines a human-readable, author-localized description for the role of an element.Element.ariaRowCountA string reflecting the
aria-rowcountattribute, which defines the total number of rows in a table, grid, or treegrid.Element.ariaRowIndexA string reflecting the
aria-rowindexattribute, which defines an element's row index or position with respect to the total number of rows within a table, grid, or treegrid.Element.ariaRowIndexTextA string reflecting the
aria-rowindextextattribute, which defines a human readable text alternative of aria-rowindex.Element.ariaRowSpanA string reflecting the
aria-rowspanattribute, which defines the number of rows spanned by a cell or gridcell within a table, grid, or treegrid.Element.ariaSelectedA string reflecting the
aria-selectedattribute, which indicates the current "selected" state of elements that have a selected state.Element.ariaSetSizeA string reflecting the
aria-setsizeattribute, which defines the number of items in the current set of listitems or treeitems.Element.ariaSortA string reflecting the
aria-sortattribute, which indicates if items in a table or grid are sorted in ascending or descending order.Element.ariaValueMaxA string reflecting the
aria-valueMaxattribute, which defines the maximum allowed value for a range widget.Element.ariaValueMinA string reflecting the
aria-valueMinattribute, which defines the minimum allowed value for a range widget.Element.ariaValueNowA string reflecting the
aria-valueNowattribute, which defines the current value for a range widget.Element.ariaValueTextA string reflecting the
aria-valuetextattribute, which defines the human-readable text alternative ofaria-valuenowfor a range widget.Element.roleA string reflecting the explicitly set
roleattribute, which provides the semantic role of the element.
Instance properties reflected from ARIA element references
The properties reflect the elements specified byid reference in the corresponding attributes, but with some caveats. SeeReflected element references in theReflected attributes guide for more information.
Element.ariaActiveDescendantElementAn element that represents the current active element when focus is on a
compositewidget,combobox,textbox,group, orapplication.Reflects thearia-activedescendantattribute.Element.ariaControlsElementsAn array of elements whose contents or presence are controlled by the element it is applied to.Reflects the
aria-controlsattribute.Element.ariaDescribedByElementsAn array of elements that contain the accessible description for the element it is applied to.Reflects the
aria-describedbyattribute.Element.ariaDetailsElementsAn array of elements that provide accessible details for the element it is applied to.Reflects the
aria-detailsattribute.Element.ariaErrorMessageElementsAn array of elements that provide an error message for the element it is applied to.Reflects the
aria-errormessageattribute.Element.ariaFlowToElementsAn array of elements that identify the next element (or elements) in an alternate reading order of content, overriding the general default reading order at the user's discretion.Reflects the
aria-flowtoattribute.Element.ariaLabelledByElementsAn array of elements that provide the accessible name for the element it is applied to.Reflects the
aria-labelledbyattribute.Element.ariaOwnsElementsAn array of elements owned by the element this is applied to.This is used to define a visual, functional, or contextual relationship between a parent and its child elements when the DOM hierarchy cannot be used to represent the relationship.Reflects the
aria-ownsattribute.
Instance methods
Element inherits methods from its parentsNode, and its own parent,EventTarget.
Element.after()Inserts a set of
Nodeobjects or strings in the children list of theElement's parent, just after theElement.Element.animate()A shortcut method to create and run an animation on an element. Returns the created Animation object instance.
Element.ariaNotify()ExperimentalNon-standardSpecifies that a given string of text should be announced by a screen reader.
Element.append()Inserts a set of
Nodeobjects or strings after the last child of the element.Element.attachShadow()Attaches a shadow DOM tree to the specified element and returns a reference to its
ShadowRoot.Element.before()Inserts a set of
Nodeobjects or strings in the children list of theElement's parent, just before theElement.Element.checkVisibility()Returns whether an element is expected to be visible or not based on configurable checks.
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.computedStyleMap()Returns a
StylePropertyMapReadOnlyinterface which provides a read-only representation of a CSS declaration block that is an alternative toCSSStyleDeclaration.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 a string.
Element.getAttributeNames()Returns an array of attribute names from the current element.
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.getAttributeNS()Retrieves the value of the attribute with the specified namespace and name from the current node and returns it as a string.
Element.getBoundingClientRect()Returns the size of an element and its position relative to the viewport.
Element.getBoxQuads()ExperimentalReturns a list of
DOMQuadobjects representing the CSS fragments of the node.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.getHTML()Returns the DOM content of the element as an HTML string, optionally including any shadow DOM.
Element.hasAttribute()Returns a boolean value indicating if the element has the specified attribute or not.
Element.hasAttributeNS()Returns a boolean value indicating if the element has the specified attribute, in the specified namespace, or not.
Element.hasAttributes()Returns a boolean value indicating if the element has one or more HTML attributes present.
Element.hasPointerCapture()Indicates whether the element on which it is invoked has pointer capture for the pointer identified by the given pointer ID.
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 boolean value indicating whether or not the element would be selected by the specified selector string.
Element.moveBefore()Moves a given
Nodeinside the invoking node as a direct child, before a given reference node, without removing and then inserting the node.Element.prepend()Inserts a set of
Nodeobjects or strings before the first child of the element.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
PointerEvent.Element.remove()Removes the element from the children list of its parent.
Element.removeAttribute()Removes the named attribute from the current node.
Element.removeAttributeNode()Removes the node representation of the named attribute from the current node.
Element.removeAttributeNS()Removes the attribute with the specified name and namespace, from the current node.
Element.replaceChildren()Replaces the existing children of a
Nodewith a specified new set of children.Element.replaceWith()Replaces the element in the children list of its parent with a set of
Nodeobjects or strings.Element.requestFullscreen()Asynchronously asks the browser to make the element fullscreen.
Element.requestPointerLock()Allows to asynchronously ask for the pointer to be locked on the given element.
Element.scroll()Scrolls to a particular set of coordinates inside a given element.
Element.scrollBy()Scrolls an element by the given amount.
Element.scrollIntoView()Scrolls the page until the element gets into the view.
Element.scrollIntoViewIfNeeded()Non-standardScrolls the current element into the visible area of the browser window if it's not already within the visible area of the browser window.Use the standard
Element.scrollIntoView()instead.Element.scrollTo()Scrolls to a particular set of coordinates inside a given element.
Element.setAttribute()Sets the value of a named attribute of the current node.
Element.setAttributeNode()Sets the node representation of the named attribute from the current node.
Element.setAttributeNodeNS()Sets the node representation of the attribute with the specified name and namespace, from the current node.
Element.setAttributeNS()Sets the value of the attribute with the specified name and namespace, from the current node.
Element.setCapture()Non-standardDeprecatedSets up mouse event capture, redirecting all mouse events to this element.
Element.setHTML()Secure contextExperimentalParses andsanitizes a string of HTML into a document fragment, which then replaces the element's original subtree in the DOM.
Element.setHTMLUnsafe()Parses a string of HTML into a document fragment, without sanitization, which then replaces the element's original subtree in the DOM. The HTML string may include declarative shadow roots, which would be parsed as template elements if the HTML was set using
Element.innerHTML.Element.setPointerCapture()Designates a specific element as the capture target of futurepointer events.
Element.toggleAttribute()Toggles a boolean attribute, removing it if it is present and adding it if it is not present, on the specified element.
Events
Listen to these events usingaddEventListener() or by assigning an event listener to theoneventname property of this interface.
afterscriptexecuteNon-standardDeprecatedFired when a script has been executed.
beforeinputFired when the value of an input element is about to be modified.
beforematchFires on an element that is in thehidden until found state, when the browser is about to reveal its content because the user has found the content through the "find in page" feature or through fragment navigation.
beforescriptexecuteNon-standardDeprecatedFired when a script is about to be executed.
beforexrselectExperimentalFired before WebXR select events (
select,selectstart,selectend) are dispatched.contentvisibilityautostatechangeFires on any element with
content-visibility: autoset on it when it starts or stops beingrelevant to the user andskipping its contents.inputFires when an element's value is changed as a direct result of a user action.
securitypolicyviolationFired when aContent Security Policy is violated.
wheelFired when the user rotates a wheel button on a pointing device (typically a mouse).
Animation events
animationcancelFired when an animation unexpectedly aborts.
animationendFired when an animation has completed normally.
animationiterationFired when an animation iteration has completed.
animationstartFired when an animation starts.
Clipboard events
Composition events
compositionendFired when a text composition system such as aninput method editor completes or cancels the current composition session.
compositionstartFired when a text composition system such as aninput method editor starts a new composition session.
compositionupdateFired when a new character is received in the context of a text composition session controlled by a text composition system such as aninput method editor.
Focus events
Fullscreen events
fullscreenchangeSent to an
Elementwhen it transitions into or out offullscreen mode.fullscreenerrorSent to an
Elementif an error occurs while attempting to switch it into or out offullscreen mode.
Keyboard events
keydownFired when a key is pressed.
keypressDeprecatedFired when a key that produces a character value is pressed down.
keyupFired when a key is released.
Mouse events
auxclickFired when a non-primary pointing device button (e.g., any mouse button other than the left button) has been pressed and released on an element.
clickFired when a pointing device button (e.g., a mouse's primary button) is pressed and released on a single element.
contextmenuFired when the user attempts to open a context menu.
dblclickFired when a pointing device button (e.g., a mouse's primary button) is clicked twice on a single element.
DOMActivateDeprecatedOccurs when an element is activated, for instance, through a mouse click or a keypress.
DOMMouseScrollDeprecatedNon-standardOccurs when mouse wheel or similar device is operated and the accumulated scroll amount is over 1 line or 1 page since last event.
mousedownFired when a pointing device button is pressed on an element.
mouseenterFired when a pointing device (usually a mouse) is moved over the element that has the listener attached.
mouseleaveFired when the pointer of a pointing device (usually a mouse) is moved out of an element that has the listener attached to it.
mousemoveFired when a pointing device (usually a mouse) is moved while over an element.
mouseoutFired when a pointing device (usually a mouse) is moved off the element to which the listener is attached or off one of its children.
mouseoverFired when a pointing device is moved onto the element to which the listener is attached or onto one of its children.
mouseupFired when a pointing device button is released on an element.
mousewheelDeprecatedNon-standardFired when a mouse wheel or similar device is operated.
MozMousePixelScrollDeprecatedNon-standardFired when a mouse wheel or similar device is operated.
webkitmouseforcechangedNon-standardFired each time the amount of pressure changes on the trackpad touch screen.
webkitmouseforcedownNon-standardFired after the mousedown event as soon as sufficient pressure has been applied to qualify as a "force click".
webkitmouseforcewillbeginNon-standardFired before the
mousedownevent.webkitmouseforceupNon-standardFired after the
webkitmouseforcedownevent as soon as the pressure has been reduced sufficiently to end the "force click".
Pointer events
gotpointercaptureFired when an element captures a pointer using
setPointerCapture().lostpointercaptureFired when acaptured pointer is released.
pointercancelFired when a pointer event is canceled.
pointerdownFired when a pointer becomes active.
pointerenterFired when a pointer is moved into the hit test boundaries of an element or one of its descendants.
pointerleaveFired when a pointer is moved out of the hit test boundaries of an element.
pointermoveFired when a pointer changes coordinates.
pointeroutFired when a pointer is moved out of thehit test boundaries of an element (among other reasons).
pointeroverFired when a pointer is moved into an element's hit test boundaries.
pointerrawupdateFired when a pointer changes any properties that don't fire
pointerdownorpointerupevents.pointerupFired when a pointer is no longer active.
Scroll events
scrollFired when the document view or an element has been scrolled.
scrollendFires when the document view has completed scrolling.
scrollsnapchangeExperimentalFired on the scroll container at the end of a scrolling operation when a new scroll snap target has been selected.
scrollsnapchangingExperimentalFired on the scroll container when the browser determines a new scroll snap target is pending, i.e., it will be selected when the current scroll gesture ends.
Touch events
gesturechangeNon-standardFired when digits move during a touch gesture.
gestureendNon-standardFired when there are no longer multiple fingers contacting the touch surface, thus ending the gesture.
gesturestartNon-standardFired when multiple fingers contact the touch surface, thus starting a new gesture.
touchcancelFired when one or more touch points have been disrupted in an implementation-specific manner (for example, too many touch points are created).
touchendFired when one or more touch points are removed from the touch surface.
touchmoveFired when one or more touch points are moved along the touch surface.
touchstartFired when one or more touch points are placed on the touch surface.
Transition events
transitioncancelAn
Eventfired when aCSS transition has been cancelled.transitionendAn
Eventfired when aCSS transition has finished playing.transitionrunAn
Eventfired when aCSS transition is created (i.e., when it is added to a set of running transitions), though not necessarily started.transitionstartAn
Eventfired when aCSS transition has started transitioning.