DOM
Interacting with the Web Page Structure
You've learned the basics of JavaScript, how to use variables, data types, and functions. Now, let's connect JavaScript to the web page itself. The Document Object Model (DOM) is your key to manipulating the content, structure, and style of a web page using JavaScript. It's the bridge between your JavaScript code and the HTML elements in the browser.
What is the DOM?
The DOM (Document Object Model) is a programming interface for HTML and XML documents. It represents the page as a tree-like structure, where each HTML element, attribute, and text node is represented as an object.
Think of the DOM as a map of your web page. It allows JavaScript to navigate the HTML structure and make changes to it in real-time.
The DOM Tree
The DOM represents the HTML document as a tree, with the document
object as the root.
Each HTML element becomes a node in the tree.
The DOM tree has a hierarchical structure:
- The
document
object is the root of the tree. - The
<html>
element is the root element of the document. - The
<head>
and<body>
elements are children of the<html>
element. - Elements within the
<head>
and<body>
are children of those elements, and so on.
Accessing Elements in the DOM
JavaScript provides several methods for accessing elements in the DOM:
document.getElementById(id)
: Returns the element with the specifiedid
attribute. This is the fastest and most efficient way to access a specific element.
document.querySelector(selector)
: Returns the first element that matches the specified CSS selector.
document.querySelectorAll(selector)
: Returns a NodeList (an array-like object) containing all elements that match the specified CSS selector.
document.getElementsByTagName(tagName)
: Returns an HTMLCollection (an array-like object) containing all elements with the specified tag name.
document.getElementsByClassName(className)
: Returns an HTMLCollection containing all elements with the specified class name.
Important Considerations:
querySelector
andquerySelectorAll
are generally preferred overgetElementsByTagName
andgetElementsByClassName
because they are more flexible and can use any CSS selector.getElementsByTagName
andgetElementsByClassName
return a live HTMLCollection, meaning that the collection is automatically updated if the DOM changes.querySelector
andquerySelectorAll
return a static NodeList, meaning that the list is not updated automatically.- Always try to use
getElementById
when possible.
Modifying Elements in the DOM
Once you have access to an element in the DOM, you can modify its content, attributes, and style:
element.textContent
: Gets or sets the text content of an element.
element.innerHTML
: Gets or sets the HTML content of an element. Use this with caution, as it can be a security risk if you're inserting user-supplied data.
element.setAttribute(attributeName, attributeValue)
: Sets the value of an attribute on the element.
element.getAttribute(attributeName)
: Gets the value of an attribute on the element.
element.style.propertyName
: Sets the value of a CSS property on the element.
element.classList
: Provides methods for adding, removing, and toggling CSS classes on an element.
Creating and Adding New Elements
You can also create new elements and add them to the DOM:
document.createElement(tagName)
: Creates a new HTML element with the specified tag name.document.createTextNode(text)
: Creates a new text node with the specified text.element.appendChild(childNode)
: Adds a child node to the end of the list of children of the element.element.insertBefore(newNode, existingNode)
: Inserts a new node before an existing node in the list of children of the element.element.removeChild(childNode)
: Removes a child node from the element.
Navigating the DOM Tree
You can navigate the DOM tree using the following properties of element nodes:
parentNode
: The parent node of the element.childNodes
: A NodeList of the element's children.firstChild
: The first child node of the element.lastChild
: The last child node of the element.nextSibling
: The next sibling node of the element.previousSibling
: The previous sibling node of the element.
Practice and Explore
The DOM is a powerful tool for manipulating web pages with JavaScript. Practice accessing, modifying, and creating elements in the DOM. Experiment with different DOM methods and properties. The more you work with the DOM, the better you'll understand how to create dynamic and interactive web experiences.
Last updated on