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>).
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.
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.
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.
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.
The features exposed by the HTML DOM are among the most commonly-used APIs in a web developer's toolkit.
All but the most simple web applications will use some features of the HTML DOM.
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.
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.
These interfaces provide structure and functionality required by the elements used to create and manage forms, including the <form> and <input> elements.
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.
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.
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 JavaScriptPromise is rejected.
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.
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.
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;}elseif(!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.
<p>Please provide the information below. Items marked with "*" are required.</p><formaction=""method="get"><p><labelfor="userName"required>Your name:</label><inputtype="text"id="userName"/> (*)
</p><p><labelfor="userEmail">Email:</label><inputtype="email"id="userEmail"/></p><inputtype="submit"value="Send"id="sendButton"/></form>
Tip: you can click/tap on a cell for more information.
Full support
Full support
Partial support
Partial support
In development. Supported in a pre-release version.
In development. Supported in a pre-release version.
No support
No support
Experimental. Expect behavior to change in the future.
Non-standard. Check cross-browser support before using.
Deprecated. Not for use in new websites.
See implementation notes.
User must explicitly enable this feature.
Has more compatibility info.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.