JavaScript HTML DOM Document
The HTML DOM document object controls all the other objects on your web page.
The HTML DOM Document Object
The document object stands for your webpage.
To access an element on an HTML page, you always begin by accessing the document object.
Here are a few instances demonstrating how you can utilize the document object to reach and control HTML.
Finding HTML Elements
Method | Description |
---|---|
document.getElementById(id) | Locate an element using its unique ID. |
document.getElementsByTagName(name) | Locate elements using the tag name. |
document.getElementsByClassName(name) | Locate elements based on their class name. |
Changing HTML Elements
Property | Description |
---|---|
element.innerHTML = new html content | Modify the content inside an HTML element. |
element.attribute = new value | Modify the value of a characteristic for an HTML element. |
element.style.property = new style | Alter the appearance of an HTML element. |
Method | Description |
element.setAttribute(attribute, value) | Modify the value of a property for an HTML element. |
Adding and Deleting Elements
Method | Description |
---|---|
document.createElement(element) | Generate an HTML element. |
document.removeChild(element) | Delete an HTML element. |
document.appendChild(element) | Insert an HTML element. |
document.replaceChild(new, old) | Change an HTML element. |
document.write(text) | Output information to the HTML display |
Adding Events Handlers
Method | Description |
---|---|
document.getElementById(id).onclick = function(){code} | Inserting code that responds to a click event. |
Finding HTML Objects
The initial HTML DOM Level 1, introduced in 1998, outlined 11 HTML elements, groups of elements, and attributes. These remain applicable in HTML5.
In HTML DOM Level 3, additional things like objects, collections, and properties were included.
Property | Description | DOM |
---|---|---|
document.anchors | Shows all <a> elements with a 'name' attribute. | 1 |
document.applets | Deprecated | 1 |
document.baseURI | Provides the exact base web address of the document. | 3 |
document.body | Provides the <body> element. | 1 |
document.cookie | Retrieves the cookie of the document. | 1 |
document.doctype | Gets the type of document. | 3 |
document.documentElement | Gives back the <html> element. | 3 |
document.documentMode | Shows the browser's preferred mode. | 3 |
document.documentURI | Provides the web address of the document. | 3 |
document.domain | Gives back the name of the server's document domain. | 1 |
document.domConfig | Obsolete. | 3 |
document.embeds | Shows all <embed> elements. | 3 |
document.forms | Shows all the <form> elements. | 1 |
document.head | Gives back the <head> part. | 3 |
document.images | Shows all <img> elements. | 1 |
document.implementation | Provides the DOM implementation. | 3 |
document.inputEncoding | Obtains the character set used in the document. | 3 |
document.lastModified | Provides information about when the document was last updated. | 3 |
document.links | Provides a list of all <area> and <a> elements containing an href attribute. | 1 |
document.readyState | Shows whether the document is currently loading or not. | 3 |
document.referrer | Provides the web address of the referring document (the linked page). | 1 |
document.scripts | Provides all <script> elements. | 3 |
document.strictErrorChecking | Returns a result when error checking is applied. | 3 |
document.title | Gives back the content inside the <title> tag. | 1 |
document.URL | Provides the entire web address of the document. | 1 |