The HTML DOM API
The HTML DOM API is made up of the interfaces that define the functionality of each of the elements in HTML, as well as any supporting types and interfaces they rely upon.
The functional areas included in the HTML DOM API include:
- Access to and control of HTML elements via the DOM.
- Access to and manipulation of form data.
- Interacting with the contents of 2D images and the context of an HTML
<canvas>
, for example to draw on top of them. - Management of media connected to the HTML media elements (
<audio>
and<video>
). - Dragging and dropping of content on webpages.
- Access to the browser navigation history
- Supporting and connective interfaces for other APIs such as Web Components, Web Storage, Web Workers, WebSocket, and Server-sent events.
HTML DOM concepts and usage
In this article, we'll focus on the parts of the HTML DOM that involve engaging with HTML elements. Discussion of other areas, such as Drag and Drop, WebSockets, Web Storage, etc. can be found in the documentation for those APIs.
Structure of an HTML document
The Document Object Model (DOM) is an architecture that describes the structure of a document
; each document is represented by an instance of the interface Document
. A document, in turn, consists of a hierarchical tree of nodes, in which a node is a fundamental record representing a single object within the document (such as an element or text node).
Nodes may be strictly organizational, providing a means for grouping other nodes together or for providing a point at which a hierarchy can be constructed; other nodes may represent visible components of a document. Each node is based on the Node
interface, which provides properties for getting information about the node as well as methods for creating, deleting, and organizing nodes within the DOM.
Nodes don't have any concept of including the content that is actually displayed in the document. They're empty vessels. The fundamental notion of a node that can represent visual content is introduced by the Element
interface. An Element
object instance represents a single element in a document created using either HTML or an XML vocabulary such as SVG.
For example, consider a document with two elements, one of which has two more elements nested inside it:
While the Document
interface is defined as part of the DOM specification, the HTML specification significantly enhances it to add information specific to using the DOM in the context of a web browser, as well as to using it to represent HTML documents specifically.
Among the things added to Document
by the HTML standard are:
- Support for accessing various information provided by the HTTP headers when loading the page, such as the location from which the document was loaded, cookies, modification date, referring site, and so forth.
- Access to lists of elements in the document's
<head>
block and body, as well as lists of the images, links, scripts, etc. contained in the document. - Support for interacting with the user by examining focus and by executing commands on editable content.
- Event handlers for document events defined by the HTML standard to allow access to mouse and keyboard events, drag and drop, media control, and more.
- Event handlers for events that can be delivered to both elements and documents; these presently include only copy, cut, and paste actions.
HTML element interfaces
The Element
interface has been further adapted to represent HTML elements specifically by introducing the HTMLElement
interface, which all more specific HTML element classes inherit from. This expands the Element
class to add HTML-specific general features to the element nodes. Properties added by HTMLElement
include for example hidden
and innerText
. HTMLElement
also adds all the global event handlers.
An HTML document is a DOM tree in which each of the nodes is an HTML element, represented by the HTMLElement
interface. The HTMLElement
class, in turn, implements Node
, so every element is also a node (but not the other way around). This way, the structural features implemented by the Node
interface are also available to HTML elements, allowing them to be nested within each other, created and deleted, moved around, and so forth.
The HTMLElement
interface is generic, however, providing only the functionality common to all HTML elements such as the element's ID, its coordinates, the HTML making up the element, information about scroll position, and so forth.
In order to expand upon the functionality of the core HTMLElement
interface to provide the features needed by a specific element, the HTMLElement
class is subclassed to add the needed properties and methods. For example, the <canvas>
element is represented by an object of type HTMLCanvasElement
. HTMLCanvasElement
augments the HTMLElement
type by adding properties such as height
and methods like getContext()
to provide canvas-specific features.
The overall inheritance for HTML element classes looks like this:
As such, an element inherits the properties and methods of all of its ancestors. For example, consider a <a>
element, which is represented in the DOM by an object of type HTMLAnchorElement
. The element, then, includes the anchor-specific properties and methods described in that class's documentation, but also those defined by HTMLElement
and Element
, as well as from Node
and, finally, EventTarget
.
Each level defines a key aspect of the utility of the element. From Node
, the element inherits concepts surrounding the ability for the element to be contained by another element, and to contain other elements itself. Of special importance is what is gained by inheriting from EventTarget
: the ability to receive and handle events such as mouse clicks, play and pause events, and so forth.
There are elements that share commonalities and thus have an additional intermediary type. For example, the <audio>
and <video>
elements both present audiovisual media. The corresponding types, HTMLAudioElement
and HTMLVideoElement
, are both based upon the common type HTMLMediaElement
, which in turn is based upon HTMLElement
and so forth. HTMLMediaElement
defines the methods and properties held in common between audio and video elements.
These element-specific interfaces make up the majority of the HTML DOM API, and are the focus of this article. To learn more about the actual structure of the DOM, see Introduction to the DOM.
HTML DOM target audience
The features exposed by the HTML DOM are among the most commonly-used APIs in the web developer's arsenal. All but the most simple web applications will use some features of the HTML DOM.
HTML DOM API interfaces
The majority of the interfaces that comprise the HTML DOM API map almost one-to-one to individual HTML elements, or to a small group of elements with similar functionality. In addition, the HTML DOM API includes a few interfaces and types to support the HTML element interfaces.
HTML element interfaces
These interfaces represent specific HTML elements (or sets of related elements which have the same properties and methods associated with them).
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDetailsElement
HTMLDialogElement
HTMLDirectoryElement
HTMLDivElement
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMenuElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLSlotElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
Deprecated HTML Element Interfaces
Obsolete HTML Element Interfaces
-
HTMLBaseFontElement
-
HTMLFontElement
-
HTMLFrameElement
-
HTMLFrameSetElement
-
HTMLIsIndexElement
-
HTMLMenuItemElement
Web app and browser integration interfaces
These interfaces offer access to the browser window and document that contain the HTML, as well as to the browser's state, available plugins (if any), and various configuration options.
Deprecated web app and browser integration interfaces
Obsolete web app and browser integration interfaces
Form support interfaces
These interfaces provide structure and functionality required by the elements used to create and manage forms, including the <form>
and <input>
elements.
Canvas and image interfaces
These interfaces represent objects used by the Canvas API as well as the <img>
element and <picture>
elements.
CanvasGradient
CanvasPattern
CanvasRenderingContext2D
ImageBitmap
ImageBitmapRenderingContext
ImageData
OffscreenCanvas
OffscreenCanvasRenderingContext2D
Path2D
TextMetrics
Media interfaces
The media interfaces provide HTML access to the contents of the media elements: <audio>
and <video>
.
AudioTrack
AudioTrackList
MediaError
TextTrack
TextTrackCue
TextTrackCueList
TextTrackList
TimeRanges
TrackEvent
VideoTrack
VideoTrackList
Drag and drop interfaces
These interfaces are used by the HTML_Drag_and_Drop_API to represent individual draggable (or dragged) items, groups of dragged or draggable items, and to handle the drag and drop process.
Page history interfaces
The History API interfaces let you access information about the browser's history, as well as to shift the browser's current tab forward and backward through that history.
Web Components interfaces
These interfaces are used by the Web Components API to create and manage the available custom elements.
Miscellaneous and supporting interfaces
These supporting object types are used in a variety of ways in the HTML DOM API. In addition, PromiseRejectionEvent
represents the event delivered when a JavaScript Promise
is rejected.
DOMStringList
DOMStringMap
ErrorEvent
HTMLAllCollection
MimeType
MimeTypeArray
PromiseRejectionEvent
Interfaces belonging to other APIs
Several interfaces are technically defined in the HTML specification while actually being part of other APIs.
Web storage interfaces
The Web_Storage_API provides the ability for web sites to store data either temporarily or permanently on the user's device for later re-use.
Web Workers interfaces
These interfaces are used by the Web_Workers_API both to establish the ability for workers to interact with an app and its content, but also to support messaging between windows or apps.
BroadcastChannel
DedicatedWorkerGlobalScope
MessageChannel
MessageEvent
MessagePort
SharedWorker
SharedWorkerGlobalScope
Worker
WorkerGlobalScope
WorkerLocation
WorkerNavigator
WebSocket interfaces
These interfaces, defined by the HTML specification, are used by the WebSockets_API.
Server-sent events interfaces
The EventSource
interface represents the source which sent or is sending server-sent events.
Examples
In this example, an <input>
element's input
event is monitored in order to update the state of a form's "submit" button based on whether or not a given field currently has a value.
JavaScript
const nameField = document.getElementById("userName"); const sendButton = document.getElementById("sendButton") sendButton.disabled = true; // [note: this is disabled since it causes this article to always load with this example focused and scrolled into view] //nameField.focus(); nameField.addEventListener("input", event => { const elem = event.target; const valid = elem.value.length != 0; if (valid && sendButton.disabled) { sendButton.disabled = false; } else if (!valid && !sendButton.disabled) { sendButton.disabled = true; } });
This code uses the Document
interface's getElementById()
method to get the DOM object representing the <input>
elements whose IDs are userName
and sendButton
. With these, we can access the properties and methods that provide information about and grant control over these elements.
The HTMLInputElement
object for the "Send" button's disabled
property is set to true
, which disables the "Send" button so it can't be clicked. In addition, the user name input field is made the active focus by calling the focus()
method it inherits from HTMLElement
.
Then addEventListener()
is called to add a handler for the input
event to the user name input. This code looks at the length of the current value of the input; if it's zero, then the "Send" button is disabled if it's not already disabled. Otherwise, the code ensures that the button is enabled.
With this in place, the "Send" button is always enabled whenever the user name input field has a value, and disabled when it's empty.
HTML
The HTML for the form looks like this:
<p>Please provide the information below. Items marked with "*" are required.</p> <form action="" method="get"> <p> <label for="userName" required>Your name:</label> <input type="text" id="userName"> (*) </p> <p> <label for="email">Email:</label> <input type="email" id="userEmail"> </p> <input type="submit" value="Send" id="sendButton"> </form>
Result
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard | Living Standard | WHATWG HTML Specification |
HTML5 | Recommendation | No change from Document Object Model (DOM) Level 2 HTML Specification |
Document Object Model (DOM) Level 2 HTML Specification | Obsolete | No change from Document Object Model (DOM) Level 1 Specification. |
Document Object Model (DOM) Level 1 Specification | Obsolete | Initial definition. |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
HTML_DOM_API |
1 |
12 |
1 |
5.5 |
8 |
1.3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
accessKey |
17 |
12 |
5 |
5.5 |
≤12.1 |
6 |
4.4 |
18 |
5 |
≤12.1 |
6 |
1.0 |
accessKeyLabel |
No |
No |
8 |
No |
No |
14 |
No |
No |
8 |
No |
14 |
No |
animationcancel_event |
No |
No |
54 |
No |
No |
13.1
12
|
No |
No |
54 |
No |
13.4
12
|
No |
animationend_event |
43 |
12 |
Yes |
10 |
30 |
9 |
43 |
43 |
Yes |
30 |
9 |
4.0 |
animationiteration_event |
43 |
12 |
51 |
10 |
30 |
9 |
43 |
43 |
51 |
30 |
9 |
4.0 |
animationstart_event |
43 |
12 |
51 |
10 |
30 |
9 |
43 |
43 |
51 |
30 |
9 |
4.0 |
attachInternals |
77 |
79 |
93 |
No |
64 |
No |
77 |
77 |
93 |
55 |
No |
12.0 |
autocapitalize |
66 |
79 |
83 |
No |
53 |
No |
66 |
66 |
83 |
47 |
10.3 |
9.0 |
autofocus |
79
1-79
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
79
12-79
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
1
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
10
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
66
≤12.1-66
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
4
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
79
≤37-79
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
79
18-79
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
4
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
57
≤12.1-57
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
3.2
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
12.0
1.0-12.0
Supported for
HTMLButtonElement , HTMLInputElement , HTMLSelectElement , and HTMLTextAreaElement . |
beforeinput_event |
Yes |
79 |
87
74-87
|
No |
Yes |
Yes |
Yes |
Yes |
87
79-87
|
Yes |
Yes |
Yes |
blur |
1 |
12 |
1.5 |
5.5 |
8 |
3 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
click |
9
Before Chrome 19,
click() is only defined on buttons and inputs. |
12 |
3
["Before Firefox 5,
click() is only defined on buttons and inputs, and has no effect on text and file inputs.", "Starting in Firefox 75, the click() function works even when the element is not attached to a DOM tree."] |
5.5 |
10.5 |
6 |
≤37
Before Android WebView 4.4,
click() is only defined on buttons and inputs. |
18
Before Chrome 19,
click() is only defined on buttons and inputs. |
4
["Before Firefox 5,
click() is only defined on buttons and inputs, and has no effect on text and file inputs.", "Starting in Firefox for Android 79, the click() function works even when the element is not attached to a DOM tree."] |
11 |
6 |
1.0
Before Samsung Internet 1.5,
click() is only defined on buttons and inputs. |
contentEditable |
1 |
12 |
3 |
5.5 |
9 |
3 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
contextMenu |
45-61 |
≤18-79 |
85
8-85
|
No |
No |
No |
45-61 |
45-61 |
85
8-85
|
No |
No |
5.0-8.0 |
dataset |
8 |
12 |
6 |
11 |
11 |
5.1 |
4.4 |
18 |
6 |
11 |
5 |
1.0 |
dir |
1 |
12 |
1 |
5.5 |
≤12.1 |
3 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
draggable |
3 |
12 |
2 |
10 |
12 |
5 |
4.4 |
18 |
4 |
12 |
4 |
1.0 |
enterKeyHint |
77 |
79 |
94
79-94
|
No |
64 |
13.1 |
77 |
77 |
94 |
55 |
13.4 |
12.0 |
focus |
1 |
12 |
1.5 |
5.5 |
8 |
3 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
gotpointercapture_event |
57 |
17 |
59 |
No |
44 |
13 |
57 |
57 |
79 |
43 |
13 |
7.0 |
hidden |
6 |
12 |
1 |
11 |
11.6 |
5.1 |
≤37 |
18 |
4 |
12 |
5 |
1.0 |
inert |
60 |
79 |
81 |
No |
47 |
No |
No |
60 |
81 |
44 |
No |
No |
innerText |
1 |
12 |
45 |
5.5 |
9.6 |
3 |
4.4 |
18 |
45 |
10.1 |
1 |
1.0 |
input_event |
1 |
79
12-79
Not supported on
select , checkbox , or radio inputs. |
6 |
9
Only supports
input of type text and password . |
11.6 |
3.1 |
1 |
18 |
6 |
12 |
2 |
1.0 |
inputMode |
66 |
79 |
77 |
No |
53 |
12.1 |
66 |
66 |
79 |
47 |
12.2 |
9.0 |
isContentEditable |
1 |
12 |
4 |
5.5 |
≤12.1 |
3 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
itemId |
17-28 |
No |
16-49 |
No |
11-15 |
No |
No |
18-28 |
16-49 |
11-14 |
No |
1.0-1.5 |
itemProp |
17-28 |
No |
16-49 |
No |
11-15 |
No |
No |
18-28 |
16-49 |
11-14 |
No |
1.0-1.5 |
itemRef |
17-28 |
No |
16-49 |
No |
11-15 |
No |
No |
18-28 |
16-49 |
11-14 |
No |
1.0-1.5 |
itemScope |
17-28 |
No |
16-49 |
No |
11-15 |
No |
No |
18-28 |
16-49 |
11-14 |
No |
1.0-1.5 |
itemType |
17-28 |
No |
16-49 |
No |
11-15 |
No |
No |
18-28 |
16-49 |
11-14 |
No |
1.0-1.5 |
itemValue |
17-28 |
No |
16-49 |
No |
11-15 |
No |
No |
18-28 |
16-49 |
11-14 |
No |
1.0-1.5 |
lang |
1 |
12 |
1 |
5.5 |
≤12.1 |
3 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
lostpointercapture_event |
57 |
17 |
59 |
No |
44 |
13 |
57 |
57 |
79 |
43 |
13 |
7.0 |
nonce |
61 |
79 |
75 |
No |
48 |
10
The property is defined only for its useful elements:
<link> , <script> , and <style> ; it is undefined for all other elements. |
61 |
61 |
79 |
45 |
10
The property is defined only for its useful elements:
<link> , <script> , and <style> ; it is undefined for all other elements. |
8.0 |
offsetHeight |
1 |
12 |
1 |
5.5 |
8 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
offsetLeft |
1 |
12 |
1 |
5.5 |
8 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
offsetParent |
1 |
12 |
1 |
5.5 |
8 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
offsetTop |
1 |
12 |
1 |
5.5 |
8 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
offsetWidth |
1 |
12 |
1 |
5.5 |
8 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
oncopy |
1 |
12 |
3 |
5.5 |
≤12.1 |
3 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
oncut |
1 |
12 |
9 |
5.5 |
≤12.1 |
3 |
1 |
18 |
9 |
≤12.1 |
1 |
1.0 |
onpaste |
1 |
12 |
9 |
5.5 |
≤12.1 |
3 |
1 |
18 |
9 |
≤12.1 |
1 |
1.0 |
outerText |
43 |
12 |
No |
5.5 |
≤12.1 |
1.3 |
43 |
43 |
No |
≤12.1 |
1 |
4.0 |
pointercancel_event |
55 |
12
12-79
|
59
29
|
11
10
|
42 |
13 |
55 |
55 |
79
29
|
42 |
13 |
6.0 |
pointerdown_event |
55 |
12
12-79
|
59
29
|
11
10
|
42 |
13 |
55 |
55 |
79
29
|
42 |
13 |
6.0 |
pointerenter_event |
55 |
12
12-79
|
59
29
|
11
10
|
42 |
13 |
55 |
55 |
79
29
|
42 |
13 |
6.0 |
pointerleave_event |
55 |
12
12-79
|
59
29
|
11
10
|
42 |
13 |
55 |
55 |
79
29
|
42 |
13 |
6.0 |
pointermove_event |
55 |
12
12-79
|
59
29
|
11
10
|
42 |
13 |
55 |
55 |
79
29
|
42 |
13 |
6.0 |
pointerout_event |
55 |
12
12-79
|
59
29
|
11
10
|
42 |
13 |
55 |
55 |
79
29
|
42 |
13 |
6.0 |
pointerover_event |
55 |
12
12-79
|
59
29
|
11
10
|
42 |
13 |
55 |
55 |
79
29
|
42 |
13 |
6.0 |
pointerrawupdate_event |
77 |
79 |
No |
No |
64 |
No |
77 |
77 |
No |
55 |
No |
12.0 |
pointerup_event |
55 |
12
12-79
|
59
29
|
11
10
|
42 |
13 |
55 |
55 |
79
29
|
42 |
13 |
6.0 |
spellcheck |
9 |
12 |
2 |
10 |
≤12.1 |
5.1 |
≤37 |
18 |
4 |
≤12.1 |
5 |
1.0 |
tabIndex |
1 |
18
12
Returns incorrect value for elements without an explicit tabindex attribute. See issue 4365703 for details.
|
1 |
5.5
Returns incorrect value for elements without an explicit tabindex attribute. See issue 4365703 for details.
|
≤12.1 |
3.1 |
4.4 |
18 |
4 |
≤12.1 |
2 |
1.0 |
title |
1 |
12 |
1 |
5.5 |
≤12.1 |
3 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
transitioncancel_event |
74 |
≤79 |
53 |
? |
62 |
13.1
12
|
74 |
74 |
53 |
53 |
13.4
12
|
11.0 |
transitionend_event |
26
1
|
≤79
≤79
|
51 |
10 |
12.1
15
11.6-15
|
7
4
|
≤37
≤37
|
26
18
|
51 |
12.1
14
12-14
|
7
3.2
|
1.5
1.0
|
transitionrun_event |
74 |
≤79 |
53 |
? |
62 |
13.1
12
|
74 |
74 |
53 |
53 |
13.4
12
|
11.0 |
transitionstart_event |
74 |
≤79 |
53 |
? |
62 |
13.1
12
|
74 |
74 |
53 |
53 |
13.4
12
|
11.0 |
translate |
19 |
79 |
No |
No |
15 |
6 |
4.4 |
25 |
No |
14 |
6 |
1.5 |
attributeStyleMap |
66 |
79 |
No |
No |
53 |
No |
66 |
66 |
No |
47 |
No |
9.0 |
style |
1 |
12 |
1 |
5.5 |
8 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
References
Guides
- Manipulating documents: A beginner's guide to manipulating the DOM.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API