Lesson 8: Advanced DOM Manipulation
This lesson explores advanced techniques for manipulating the DOM, including creating, removing, and cloning elements, and managing element classes and attributes more effectively.
Creating Elements
1. Creating and Appending Elements
Create new elements and append them to the DOM using JavaScript.
const newElement = document.createElement("div"); newElement.innerHTML = "I am a new element!"; document.body.appendChild(newElement);
Example of Creating and Appending Elements:
const newDiv = document.createElement("div"); newDiv.innerHTML = "This is a dynamically added div."; document.body.appendChild(newDiv);
Removing Elements
1. Removing an Element
Remove elements from the DOM using the `removeChild` or `remove` methods.
const elementToRemove = document.getElementById("elementId"); elementToRemove.remove(); // or elementToRemove.parentNode.removeChild(elementToRemove);
Example of Removing an Element:
<div id="removeMe">Remove me</div> const element = document.getElementById("removeMe"); element.remove();
Cloning Elements
1. Cloning an Element
Clone elements using the `cloneNode` method. You can choose to clone the element with or without its child nodes.
const originalElement = document.getElementById("original"); const clone = originalElement.cloneNode(true); document.body.appendChild(clone);
Example of Cloning an Element:
<div id="original">I am the original!</div> const original = document.getElementById("original"); const clone = original.cloneNode(true); document.body.appendChild(clone);
Managing Classes
1. Adding and Removing Classes
Use the `classList` property to add, remove, and toggle classes on elements.
const element = document.getElementById("myElement"); element.classList.add("newClass"); element.classList.remove("oldClass"); element.classList.toggle("toggleClass");
Example of Managing Classes:
<div id="myElement">Manage my classes!</div> const element = document.getElementById("myElement"); element.classList.add("highlight"); element.classList.remove("lowlight"); element.classList.toggle("active");
Managing Attributes
1. Setting and Getting Attributes
Use `setAttribute` and `getAttribute` to manage element attributes.
const element = document.getElementById("myElement"); element.setAttribute("data-info", "someData"); const info = element.getAttribute("data-info");
Example of Managing Attributes:
<div id="myElement">Attribute Management</div> const element = document.getElementById("myElement"); element.setAttribute("data-role", "admin"); const role = element.getAttribute("data-role"); console.log(role); // Output: admin
Assignment
Create a program that:
- Creates a form where users can input text. When submitted, the form should dynamically add the input as a new list item to an unordered list.
- Provides a button that, when clicked, clones a section of the page and appends the clone to another part of the page.
- Allows users to toggle between different themes by adding and removing classes from the body element.