Managing DOM Elements in JavaScript
Example Code
Below is an example of creating, updating, and deleting elements in the DOM:
<!DOCTYPE html>
<html lang="en">
<head>
<title>DOM Example</title>
</head>
<body>
<h1 id="heading">JS Course By DeathCode</h1>
<ul id="fruits">
<li id="fruit">Mango</li>
<li id="fruit">Orange</li>
<li id="fruit">Grapes</li>
</ul>
<script src="index.js"></script>
</body>
</html>
Creating, Updating, and Deleting Elements
1. Creating New Elements
To create a new element, use the document.createElement
method. You can then set attributes or content and append it to the DOM.
function addFruit(name) {
const fruits = document.getElementById("fruits");
const li = document.createElement('li');
li.setAttribute("id", "fruit");
li.textContent = name;
fruits.appendChild(li);
}
addFruit("Strawberry");
addFruit("Blackberry");
2. Updating Elements
You can update the content or attributes of an existing element dynamically.
function changeFruitsColor(color) {
const fruits = document.querySelectorAll("#fruit");
fruits.forEach(ele => {
ele.style.color = color;
});
}
changeFruitsColor("yellow");
3. Deleting Elements
To remove an element, use the remove
method or replace it with another element.
const lastFruit = document.querySelector("ul li:last-child");
lastFruit.remove();
const fruit = document.querySelector("ul li:nth-child(2)");
const li = document.createElement('li');
li.textContent = "Blackberry";
fruit.replaceWith(li);
NodeList vs HTMLCollection
NodeList and HTMLCollection are both collections of DOM elements, but they have key differences:
- NodeList: A static or live list of nodes. Can be iterated using
forEach
(if static). - HTMLCollection: A live collection of elements that updates automatically when the DOM changes. Cannot be iterated directly with
forEach
.
Neither of these are true JavaScript arrays, so methods like map
or filter
are not directly available. You can convert them to arrays using Array.from
or the spread operator.
// Converting NodeList to Array
const nodeList = document.querySelectorAll("li");
const nodeArray = Array.from(nodeList);
nodeArray.forEach(node => console.log(node.textContent));
// Converting HTMLCollection to Array
const htmlCollection = document.getElementsByTagName("li");
const htmlArray = [...htmlCollection];
htmlArray.forEach(node => console.log(node.textContent));
Playlist of JS-Journey (JavaScript)
1. Introduction to JavaScript - What, Why, and How? | JavaScript Journey | In Hindi | Death CodeWatch
2. Variables & Comments in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
3. Understanding Data Types in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
4. Master Type Conversion & Reference Data Types π | JavaScript Journey | In Hindi | Death CodeWatch
5. Mastering Operators in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
6. Mastering Numbers & Math Functions π | JavaScript Journey | In Hindi | Death CodeWatch
7. Strings and String Methods in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
8. Mastering Dates and Date Methods in JavaScript β³ | JavaScript Journey | In Hindi | Death CodeWatch
9. JavaScript Arrays & Array Methods Explained β³ | JavaScript Journey | In Hindi | Death CodeWatch
10. Objects and Object Methods Explained π | JavaScript Journey | In Hindi | Death CodeWatch
11. Conditional Statements | If, Switch & Ternary Operator | JavaScript Journey | In Hindi | Death CodeWatch
12. Mastering Loops (for, while, do-while) | JavaScript Journey | In Hindi | Death CodeWatch
13. Understanding Functions | JavaScript Journey | In Hindi | Death CodeWatch
14. HOFs, Callback Functions, Arrow Functions & More! π | JavaScript Journey | In Hindi | Death CodeWatch
15. Mastering Scope & Hoisting Explained in Hindi π₯ | JavaScript Journey | In Hindi | Death CodeWatch
16. Map, Filter, Reduce & forEach Explainedπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
17. Understanding this Keyword, Function Context & IIFEπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
18. Dive into Call Stack & JavaScript Execution Contextπ₯ | JavaScript Journey | In Hindi | Death CodeWatch
19. Master Async Await & Event Loop π₯ | JavaScript Journey | In Hindi | Death CodeWatch
20. Promises in JavaScript Made Easy π | JavaScript Journey | In Hindi | Death CodeWatch
21. Fetch Function in JavaScript π | JavaScript Journey | In Hindi | Death CodeWatch
22. JavaScript OOP Basics π | JavaScript Journey | In Hindi | Death CodeWatch
23. Mastering Prototypes & Inheritance in JS π | JavaScript Journey | In Hindi | Death CodeWatch
24. JavaScript Classes & Inheritance Explained! π | JavaScript Journey | In Hindi | Death CodeWatch
25. Mastering Getter and Setter (Why & How to use them?) π | JavaScript Journey | In Hindi | Death CodeWatch
26. JavaScript Property Descriptors π | JavaScript Journey | In Hindi | Death CodeWatch
27. DOM Basics π | JavaScript Journey | In Hindi | Death CodeWatch
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeCurrently Playing
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch