Key takeaways:
The Document Object Model (DOM) is a virtual layout that displays the structure and content of a file, shows the elements in it, and allows alterations to them.
The HTML DOM is a DOM applied to an HTML file, also known as a web page.
HTML elements are objects that are part of a collection of nodes. They act as points within an interconnected network. The easiest way to locate an HTML element is with its ID, which is unique and cannot be replicated.
The JavaScript HTML DOM is an HTML DOM that is accessed using the programming language JavaScript. Alterations to the DOM include creating, deleting, and changing the elements.
Imagine a webpage as a tree, where each element like headers, paragraphs, links, and images are branches. With JavaScript, we can “climb” this tree to access and modify any element or structure on the page. The DOM allows JavaScript to interact dynamically with the content, structure, and style of a webpage, creating a seamless experience for users by allowing real-time updates and changes to the webpage without reloading it.
Introduction to the HTML DOM
The HTML DOM (Document Object Model) is a programming interface provided by browsers that allows JavaScript to interact with HTML documents. It acts as a map for the webpage, where each HTML element becomes an object, creating a tree-like structure. This structure lets JavaScript add, delete, modify, or respond to user interactions in real time, making the web experience dynamic.
The DOM isn’t limited to JavaScript; other languages, like Python or Java, can also manipulate the DOM. However, JavaScript is the most commonly used for client-side web interactions.
DOM structure and terminology
The HTML document is structured as a tree, where each HTML element (tag) represents a node. These nodes form relationships:
Document node: The root node representing the entire HTML document.
Element nodes: HTML tags like <html>
, <body>
, <div>
, etc., each becoming an individual node.
Text nodes: The text inside HTML tags becomes a text node, holding actual content.
Attributes: Elements can have attributes (like class
or id
) that provide additional properties.
Example
Let’s have an example of a simple HTML structure and its corresponding DOM tree: