The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a document as a tree of objects that can be modified with scripting languages like JavaScript.
To access an HTML element, JavaScript uses the document.getElementById() method. This method returns the element with the specified ID.
// Example: Accessing an element by ID
let myElement = document.getElementById('myElementId');
You can change the content of an HTML element using the innerHTML property or modify its attributes.
// Example: Changing the content of an element
let myElement = document.getElementById('myElementId');
myElement.innerHTML = 'New Content';
// Example: Modifying an attribute
myElement.setAttribute('src', 'newImage.jpg');
You can change the style of an HTML element using the style property. This property allows you to set CSS properties directly.
// Example: Changing the style of an element
let myElement = document.getElementById('myElementId');
myElement.style.color = 'red';
myElement.style.fontSize = '24px';
Events are actions that happen to HTML elements. JavaScript can be used to trigger events or respond to them.
// Example: Adding an event listener
let myButton = document.getElementById('myButtonId');
myButton.addEventListener('click', function() {
alert('Button Clicked!');
});
You can create new HTML elements using JavaScript with the document.createElement() method.
// Example: Creating a new element
let newElement = document.createElement('div');
newElement.innerHTML = 'New Element';
document.body.appendChild(newElement);
Elements can be added or removed from the DOM using methods like appendChild() and removeChild().
// Example: Adding an element
let parentElement = document.getElementById('parentElementId');
let newElement = document.createElement('div');
parentElement.appendChild(newElement);
// Example: Removing an element
let elementToRemove = document.getElementById('elementToRemoveId');
elementToRemove.parentNode.removeChild(elementToRemove);
You can add, remove, or toggle classes on elements using the classList property.
// Example: Adding a class
let myElement = document.getElementById('myElementId');
myElement.classList.add('myClass');
// Example: Removing a class
myElement.classList.remove('myClass');
// Example: Toggling a class
myElement.classList.toggle('active');
Traversing means moving up, down, and sideways in the DOM tree. You can navigate using properties like parentNode and children.
// Example: Navigating up the DOM tree
let myElement = document.getElementById('myElementId');
let parentElement = myElement.parentNode;
// Example: Navigating down the DOM tree
let children = parentElement.children;
// Example: Navigating sideways in the DOM tree
let firstSibling = myElement.nextElementSibling;
let previousSibling = myElement.previousElementSibling;
The DOM is a powerful tool for manipulating web pages. With JavaScript, you can dynamically change content, respond to user actions, and create interactive web applications.