The **Document Object Model (DOM)** is a programming interface for web documents. It represents the structure of an HTML document as a tree of objects that can be manipulated with JavaScript. DOM manipulation allows developers to dynamically update the content, structure, and style of a webpage.
DOM manipulation refers to using JavaScript to change or interact with elements in the DOM. This includes adding, removing, or modifying HTML elements, attributes, and styles.
Think of DOM as a way to programmatically control the content of a webpage.
The first step in DOM manipulation is selecting the element(s) you want to manipulate. Here are common methods to select elements:
// Selecting by ID
const elementById = document.getElementById('myElement');
// Selecting by class
const elementsByClass = document.getElementsByClassName('myClass');
// Selecting by tag
const elementsByTag = document.getElementsByTagName('div');
// Using querySelector (recommended)
const singleElement = document.querySelector('.myClass');
const allElements = document.querySelectorAll('.myClass');
Use querySelector
and querySelectorAll
for modern and flexible element selection.
You can change the content of an element using innerHTML
or textContent
.
const element = document.getElementById('myElement');
// Changing the HTML content
element.innerHTML = 'This is bold text.';
// Changing plain text
element.textContent = 'This is plain text.';
Use textContent
when working with plain text to avoid injecting unintended HTML.
You can add, remove, or change attributes of an element.
const element = document.querySelector('img');
// Setting a new attribute
element.setAttribute('src', 'image.jpg');
// Getting an attribute
console.log(element.getAttribute('src'));
// Removing an attribute
element.removeAttribute('alt');
Manipulate attributes to dynamically update element properties like src
, href
, or alt
.
You can create new elements, append them to the DOM, or remove existing elements.
const newElement = document.createElement('div');
newElement.textContent = 'I am a new element';
// Appending the element to the body
document.body.appendChild(newElement);
// Removing an element
const elementToRemove = document.getElementById('removeMe');
elementToRemove.remove();
Use createElement
and appendChild
to add elements, and remove
to delete them.
You can modify the styles of an element using the style
property.
const element = document.querySelector('.box');
// Changing the style
element.style.backgroundColor = 'blue';
element.style.color = 'white';
element.style.padding = '10px';
Directly modify the style
property for quick styling updates.
You can make your webpage interactive by adding event listeners to elements.
const button = document.querySelector('button');
button.addEventListener('click', () => {
alert('Button was clicked!');
});
Use addEventListener
to handle events like clicks, mouseovers, or keypresses.
DOM manipulation is a powerful feature of JavaScript that lets you create dynamic and interactive webpages. By mastering element selection, content modification, styling, and event handling, you can build engaging user interfaces with ease.