DOM Manipulation in Javascript
The DOM (Document Object Model) in JavaScript is a programming interface that represents the structure and content of a web page as objects. It allows JavaScript to access, manipulate, and interact with HTML and XML documents dynamically. The DOM provides a way to programmatically change the content, structure, and style of a web page, making it a powerful tool for creating interactive and dynamic web applications.
Key Concepts of DOM in JavaScript:
- Document: The `document` object represents the entire HTML document and serves as the entry point to the DOM. It provides methods and properties to access and manipulate elements within the document.
- Elements: HTML elements such as `<div>`, `<p>`, `<h1>`, etc., are represented as objects in the DOM. You can access elements using methods like `getElementById`, `querySelector`, `querySelectorAll`, etc.
- Attributes: Attributes of HTML elements, such as `id`, `class`, `src`, `href`, etc., are also represented as objects in the DOM. You can read and modify attributes using appropriate DOM methods.
- Nodes: All elements and text within an HTML document are represented as nodes in the DOM. The DOM tree consists of various types of nodes, including element nodes, text nodes, comment nodes, etc.
- Event Handling: The DOM allows you to attach event listeners to elements to respond to user interactions like clicks, keypresses, mouse movements, etc. This enables you to create interactive user experiences.
Basic DOM Manipulation in JavaScript:
- Accessing Elements:
Β Β – `document.getElementById(‘elementId’)`: Retrieves an element by its unique ID attribute.
Β Β – `document.querySelector(‘selector’)`: Returns the first element that matches the specified CSS selector.
Β Β – `document.querySelectorAll(‘selector’)`: Returns a collection of all elements that match the specified CSS selector.
Β
- Modifying Content:
Β Β – `element.innerHTML`: Gets or sets the HTML content of an element.
Β Β – `element.textContent`: Gets or sets the text content of an element (ignoring HTML tags).
Β Β – `element.setAttribute(‘attributeName’, ‘attributeValue’)`: Sets the value of an attribute for an element.
Β
- Creating and Removing Elements:
Β Β – `document.createElement(‘elementName’)`: Creates a new element node.
Β Β – `parentElement.appendChild(newElement)`: Appends a new element as a child of the parent element.
Β Β – `parentElement.removeChild(childElement)`: Removes a child element from the parent element.
Β
- Styling Elements:
Β Β – `element.style.property = ‘value’`: Changes the inline CSS style of an element.
Β
- Event Handling:
Β Β – `element.addEventListener(‘eventName’, eventHandlerFunction)`: Attaches an event listener to an element, triggering the specified function when the event occurs.
With DOM manipulation in JavaScript, you can dynamically update the content, structure, and behavior of your web page to create a more interactive user experience.
π Rate and Share Your Thoughts! π
π¦ΈββοΈ Happy Learning! π₯
Good basics for DOM manipulation with JS, could add quite a few more to accessing elements and style property, A executable example link would be nice for first time viewers, stackblitz is one option for that. Overall a good short read for beginners
Thanks for your valuable feedback!