DOM Manipulation

The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. DOM Manipulation is an essential skill in web development, allowing you to dynamically update content, structure, and styling of web pages.

Accessing Elements

You can access elements in the DOM using various methods. Here are some common ways:

Example: getElementById
Try yourself
        
            // Accessing an element by its ID and changing its content
const element = document.getElementById('example');
element.innerHTML = 'New Content';
        
    

This example demonstrates how to use getElementById to access and modify an element by its ID.


Example: querySelector
Try yourself
        
            // Accessing the first element that matches a CSS selector
const element = document.querySelector('.example');
element.style.color = 'red';
        
    

This example shows how to use querySelector to select the first element that matches a CSS selector.


Modifying Elements

Once you have accessed elements, you can modify their properties, attributes, and content using various methods.

Example: Modifying innerHTML
Try yourself
        
            // Modifying the HTML content inside an element
const element = document.getElementById('example');
element.innerHTML = '<p>New HTML Content</p>';
        
    

This example shows how to modify the HTML content inside an element using innerHTML.


Example: Modifying Attributes
Try yourself
        
            // Setting and removing attributes on an element
const element = document.getElementById('example');
element.setAttribute('data-example', 'true');
element.removeAttribute('data-example');
        
    

This example demonstrates how to set and remove attributes on an element using setAttribute and removeAttribute.


Event Handling

You can add interactivity to your web pages by attaching event listeners to elements. Events are actions or occurrences that happen in the system you are programming, which the system tells you about so your code can react to them.

Example: Click Event
Try yourself
        
            // Attaching a click event listener to an element
const button = document.getElementById('exampleButton');
button.addEventListener('click', () => {
    alert('Button clicked!');
});
        
    

This example shows how to attach a click event listener to an element using addEventListener.


Creating and Removing Elements

You can dynamically create and remove elements from the DOM using methods like createElement, appendChild, and removeChild.

Example: Creating Elements
Try yourself
        
            // Creating a new element and appending it to the DOM
const newElement = document.createElement('div');
newElement.innerHTML = 'I am a new element';
document.body.appendChild(newElement);
        
    

This example shows how to create a new element and append it to the DOM using createElement and appendChild.


Example: Removing Elements
Try yourself
        
            // Removing an element from the DOM
const elementToRemove = document.getElementById('example');
elementToRemove.parentNode.removeChild(elementToRemove);
        
    

This example demonstrates how to remove an element from the DOM using removeChild.


Important Notes

Here are some important notes and best practices for DOM Manipulation: