Understanding the HTML DOM in JavaScript
What is the DOM?
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 manipulated using JavaScript. This allows developers to dynamically change the content, structure, and style of a webpage.
Why Use the DOM?
- To dynamically update content on a webpage without reloading.
- To create interactive and responsive web applications.
- To access and manipulate HTML elements programmatically.
Common DOM Methods
document.getElementById()
1. Retrieves an element by its ID.
const h1 = document.getElementById("heading");
h1.style.color = "#fff"; // Changes text color to white
document.querySelector()
2. Returns the first element that matches a CSS selector.
const para = document.querySelector("#para");
console.log(para.innerText); // Logs visible text of the paragraph
document.querySelectorAll()
3. Returns a NodeList of all elements matching a CSS selector.
const allParagraphs = document.querySelectorAll("p");
allParagraphs.forEach((p, index) => {
console.log(`Paragraph ${index + 1}:`, p.textContent);
});
document.getElementsByClassName()
4. Retrieves all elements with a specified class name.
const highlighted = document.getElementsByClassName("highlight");
console.log(highlighted[0]); // Logs the first element with class 'highlight'
5. Manipulating the Document Title
document.title = "Death Code DOM Tutorial";
6. Adding a New Element
We can dynamically create and add new elements to the DOM.
const newPara = document.createElement("p");
newPara.textContent = "This is a dynamically added paragraph.";
document.body.appendChild(newPara);
Additional Tips
- Always use
querySelector
orquerySelectorAll
for modern projects as they are more versatile. - Use
innerText
for visible text andtextContent
for all text, including hidden content. - Manipulate styles dynamically using the
style
property. - Use
createElement
andappendChild
to add new elements to your webpage.
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 CodeCurrently Playing
28. How to Create, Update, and Delete DOM Elements π | JavaScript Journey | In Hindi | Death CodeWatch
29. JavaScript Events & Propagation π | JavaScript Journey | In Hindi | Death CodeWatch