๐ฅDOM Kya Hai? JavaScript se Web Page Pe Jadoo Kaise Dikhayein (DOM Manipulation Explained!) ๐ฅ


๐ Introduction:
Socho tumhare saamne ek webpage hai โ uska har button, image, text box tumhare control me hoโฆ sirf JavaScript ke ek line se. Yahi hota hai DOM ka jaadu.
Agar web development me naye ho, toh DOM (Document Object Model) ko samajhna ek game-changer ho sakta hai.
๐ก DOM Kya Hai? (Simple Explanation)
DOM ka full form hota hai Document Object Model.
Jab browser tumhara HTML code read karta hai, toh wo usko ek Tree Structure me convert karta hai jise hum DOM Tree kehte hain. Har HTML element ek Node ban jaata hai.
<html>
<body>
<h1>Hello World</h1>
<p>This is a paragraph</p>
</body>
</html>
Ye browser ke andar kuch aise dikhega (DOM Tree):
- html
โโโ body
โโโ h1 โ "Hello World"
โโโ p โ "This is a paragraph"
๐ง DOM Kaam Kaise Karta Hai?
Jab tum JavaScript likhte ho jaise:
document.querySelector("h1").textContent = "Namaste Duniya!";
Toh ye h1 node ko DOM tree me dhoondhta hai aur uska content change kar deta hai.
๐ ๏ธ DOM Ko Manipulate Kaise Karte Hain?
1. Selecting Elements
document.getElementById("myId")
document.querySelector(".myClass")
2. Changing Text / HTML
element.textContent = "Naya Text";
element.innerHTML = "<strong>Bold Text</strong>";
3. Changing Styles
element.style.color = "red";
4. Adding / Removing Classes
element.classList.add("active");
element.classList.remove("hidden");
5. Creating New Elements
const newDiv = document.createElement("div");
newDiv.textContent = "Main naya hoon!";
document.body.appendChild(newDiv);
๐ Real Life Example: Button Click Pe Text Change
<button id="btn">Click Me!</button>
<p id="text">Original Text</p>
<script>
const btn = document.getElementById("btn");
const text = document.getElementById("text");
btn.addEventListener("click", () => {
text.textContent = "Button Clicked!";
});
</script>
๐ DOM Ke Kuch Important Concepts:
Concept | Use |
document | DOM tree ka root |
Node | DOM ka ek element |
Event Listeners | User interaction ka response |
Traversal | Parent, child, sibling dhoondhna |
๐ฅ DOM Ko Acche Se Seekhne Ke Tips:
Practice Karo: Ek chhota sa HTML page lo aur JavaScript se modify karo.
Console Use Karo:
console.log(document.body)
se tree explore karo.Live Projects Pe Kaam Karo: Jaise todo-list, theme changer, popup creator, etc.
๐ Closing Line:
Agar tumko DOM samajh aaya ho ya ab bhi confusion ho, toh comment karke puchhna mat bhoolna.
Next blog me hum "Event Delegation" ya "DOM Traversal Deep Dive" karenge โ toh jude raho!
Subscribe to my newsletter
Read articles from Vivek varshney directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Vivek varshney
Vivek varshney
Full-Stack Web Developer | Blogging About Tech, React.js, Angular, Node.js & Web Development. Turning Ideas into Code & Helping Businesses Grow!