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

Vivek varshneyVivek varshney
2 min read

๐Ÿ“• 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:

ConceptUse
documentDOM tree ka root
NodeDOM ka ek element
Event ListenersUser interaction ka response
TraversalParent, child, sibling dhoondhna

๐Ÿ’ฅ DOM Ko Acche Se Seekhne Ke Tips:

  1. Practice Karo: Ek chhota sa HTML page lo aur JavaScript se modify karo.

  2. Console Use Karo: console.log(document.body) se tree explore karo.

  3. 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!

0
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!