DOM Manipulation and Interactions
The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a web page, where each element (like paragraphs, headings, buttons, etc.) is an object that can be manipulated using JavaScript. DOM manipulation allows you to dynamically modify the content, structure, and style of a web page without reloading it. By interacting with the DOM, you can create highly interactive and responsive websites.
In this article, we’ll explore the basics of DOM manipulation and how to create meaningful interactions using JavaScript.
What is the DOM?
The DOM is a hierarchical representation of a web page’s structure, where each part of the page (such as elements, attributes, and text) is an object. JavaScript provides methods and properties to access and manipulate these DOM objects, making it possible to update content, change styles, add or remove elements, and handle user interactions dynamically.
When a web page loads, the browser creates a DOM for that page, which is a tree of objects. You can access and modify these objects using JavaScript.
Accessing DOM Elements
To manipulate the DOM, you first need to access the elements that you want to change. JavaScript provides various methods to select elements from the DOM.
1. getElementById()
This method returns the element with the specified id
attribute.
In this example, the element with the id="mainHeading"
is selected, and its text content is updated.
2. getElementsByClassName()
This method returns a collection of elements with a specific class name.
Since getElementsByClassName()
returns a live HTMLCollection, you can modify any of the elements in the collection by referencing their index.
3. querySelector()
The querySelector()
method returns the first element that matches a specified CSS selector.
4. querySelectorAll()
The querySelectorAll()
method returns all elements that match a given CSS selector, as a static NodeList.
Manipulating DOM Elements
Once you have selected the DOM elements, you can manipulate their content, attributes, and styles.
1. Changing Text Content
To change the text content of an element, you can use the textContent
property.
Alternatively, you can use innerHTML
if you want to modify HTML content inside an element.
2. Modifying Attributes
You can modify an element's attributes using the setAttribute()
method.
You can also get an attribute using getAttribute()
.
3. Changing Styles
You can change the styles of elements by modifying the style
property directly.
Creating and Removing Elements
DOM manipulation also allows you to dynamically create and remove elements on the web page.
1. Creating New Elements
To create a new HTML element, you can use document.createElement()
. After creating an element, you can append it to an existing element in the DOM.
You can also create and append multiple elements.
2. Removing Elements
To remove an element, you first need to select it, then call the remove()
method.
Alternatively, if you want to remove an element's parent, you can use parentNode.removeChild()
.
Event Handling and Interactions
DOM manipulation is often paired with event handling to respond to user actions such as clicks, keystrokes, and mouse movements.
1. Adding Event Listeners
To handle events, you can add event listeners to DOM elements. Event listeners are functions that run when an event occurs (such as a click).
In this example, when the user clicks the button with the id="submitButton"
, the function inside the addEventListener()
is executed, displaying an alert.
2. Event Object
The event object is automatically passed to the event handler function. It contains details about the event that occurred, such as the type of event, the element that triggered the event, and the position of the mouse.
3. Removing Event Listeners
To stop an event listener from firing, you can use the removeEventListener()
method. This requires the same function reference that was used when adding the event listener.
Animating DOM Elements
You can also manipulate the DOM to create animations, such as moving elements or fading them in and out. Although CSS animations are often preferred for performance reasons, JavaScript provides more control for interactive or dynamic animations.
For example, to change an element’s position over time:
In this example, the moveBox()
function changes the left
style of the box, moving it across the screen.
Last updated
Was this helpful?