DOM Manipulation
DOM (Document Object Model) manipulation refers to the process of modifying the structure, content, or appearance of a web page using JavaScript. The DOM represents the HTML document as a tree-like structure, where each element, attribute, and text node is an object that can be accessed, modified, or created using JavaScript. Here are the key aspects of DOM manipulation:
Accessing Elements:
To manipulate elements in the DOM, you first need to access them. This can be done using methods like getElementById, getElementsByClassName, getElementsByTagName, or more advanced methods like querySelector and querySelectorAll. These methods return element objects that you can then interact with.
// Access an element by its ID const element = document.getElementById('myElement'); // Access multiple elements by class name const elements = document.getElementsByclass('myClass'); // Access elements by tag name const elements = document.getElementsByTagName('div'); // Access an element using CSS selector const element = document.querySelector('.myClass'); // Access multiple elements using CSS selector const elements = document.querySelectorAll('div');
Modifying Content and Attributes: Once you have accessed an element, you can modify its content, attributes, or style properties. Common methods and properties used for this purpose include innerHTML, textContent, setAttribute, getAttribute, classList, and style.
// Modify innerHTML to change the content element.innerHTML = 'New content'; // Modify textContent to change the text element.textContent = 'New text'; // Set attribute value element.setAttribute('src', 'image.jpg'); // Get attribute value const src = element.getAttribute('src'); // Manipulate CSS classes element.classList.add('newClass'); element.classList.remove('oldClass'); element.classList.toggle('active'); // Modify style properties element.style.color = 'red'; element.style.fontSize = '16px';
Creating and Appending Elements: You can create new elements dynamically using the createElement method, set their properties, and append them to the document or other elements.
// Create a new element const newElement = document.createElement('div'); // Set properties and attributes newElement.innerHTML = 'New element'; newElement.setAttribute('class', 'newClass'); // Append to the document or another element document.body.appendChild(newElement); // Appending to the body const parentElement = document.getElementById('parent'); parentElement.appendChild(newElement); // Appending to a specific parent element
Event Handling: DOM manipulation often involves handling user interactions and responding to events such as clicks, keypresses, or form submissions. You can attach event listeners to elements using methods like addEventListener to execute code when specific events occur.
// Attach click event listener element.addEventListener('click', function(event) { // Code to execute on click }); // Attach keypress event listener element.addEventListener('keypress', function(event) { // Code to execute on keypress }); // Attach form submission event listener formElement.addEventListener('submit', function(event) { event.preventDefault(); // Prevent form submission // Code to execute on form submission });
DOM manipulation allows you to create dynamic and interactive web pages. By accessing and modifying elements in the DOM, you can update content, respond to user actions, create new elements, and manipulate styles and attributes. Keep in mind that excessive or inefficient DOM manipulation can impact performance, so it's important to use it judiciously and optimize where possible.