0% found this document useful (0 votes)
4 views10 pages

Complete JavaScript DOM Guide

The document provides comprehensive notes on JavaScript and the Document Object Model (DOM), covering basics like variable declarations (var, let, const) and various DOM methods for accessing, manipulating, and managing HTML elements. It includes explanations of common mistakes in DOM and JavaScript usage, emphasizing the importance of correct syntax, element existence, and proper event handling. The author offers to create a practice file to help users understand these concepts through live examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Complete JavaScript DOM Guide

The document provides comprehensive notes on JavaScript and the Document Object Model (DOM), covering basics like variable declarations (var, let, const) and various DOM methods for accessing, manipulating, and managing HTML elements. It includes explanations of common mistakes in DOM and JavaScript usage, emphasizing the importance of correct syntax, element existence, and proper event handling. The author offers to create a practice file to help users understand these concepts through live examples.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Dr5cfSamajh gaya 👍 Solaiman bhai, aapko poore complete notes chahiye

— jismein:

1. DOM ke saare methods/properties ho, koi bhi chhuta na ho.


2. Har ek method ko easy words mein explain karna ho ke kab use
karna hai.
3. let aur const (basic JS part jo DOM ke saath use hote hain) bhi cover
karne hain.

Chaliye ab main aapko full + detailed + easy notes banata hoon.

📘 Complete Notes – JavaScript


DOM (with Basics)

1. JavaScript Basics (Foundation jo DOM ke


saath use hoti hai)
var, let, const

 var → purane JavaScript mein use hota tha (ab avoid karte hain).
 let → variable banane ke liye (value badal sakte ho).
 const → constant banane ke liye (value ek baar set ho gayi, badal
nahi sakte).

👉 Example:

let name = "Ali"; // change ho sakta hai


const pi = 3.14159; // constant hai, badal nahi sakte

Kab use karein?

 Jab value change karni ho → let


 Jab value fix ho → const

2. DOM Kya Hai?


DOM = Document Object Model.

 Har HTML element ek object ban jaata hai.


 JavaScript us object ko select, change, add, delete kar sakta hai.

👉 Simple line: DOM = bridge between JavaScript aur HTML/CSS.

3. DOM Access Methods (Elements ko


dhoondhne ke liye)
(a) [Link]("id")

 Ek element return karta hai jiski id di ho.


 Kab use karein? Jab ek unique element chahiye (id hamesha unique
hoti hai).

let el = [Link]("header");

(b) [Link]("class")

 List of elements return karta hai (HTMLCollection).


 Kab use karein? Jab ek hi class ke multiple elements ho.

let boxes = [Link]("box");

(c) [Link]("tag")

 List of elements based on tag.


 Kab use karein? Jab ek type ke saare elements chahiye (jaise saare
<p>).

let paras = [Link]("p");

(d) [Link]("selector")

 Sirf pehla element return karta hai jo CSS selector se match kare.
 Kab use karein? Jab sirf ek element chahiye.

let firstBox = [Link](".box");


(e) [Link]("selector")

 Saare elements (NodeList) return karta hai jo CSS selector match


kare.
 Kab use karein? Jab ek se zyada elements ko select karna ho.

let allBoxes = [Link](".box");

4. DOM Content Manipulation


(a) innerHTML

 Element ke andar ka HTML code change ya read karta hai.


 Kab use karein? Jab HTML tags bhi change karne ho.

[Link]("demo").innerHTML = "<b>Bold text</b>";

(b) innerText

 Sirf text content change ya read karta hai.


 Kab use karein? Jab plain text chahiye.

[Link]("demo").innerText = "Hello World";

(c) textContent

 Similar to innerText but hidden text bhi read karega.


 Kab use karein? Jab saara text chahiye (even hidden).

5. DOM Attributes
(a) getAttribute("attr")

 Attribute ka value nikalta hai.

let src = [Link]("img1").getAttribute("src");

(b) setAttribute("attr","value")

 Attribute set karta hai.


[Link]("img1").setAttribute("src","[Link]");

6. DOM CSS Manipulation


(a) [Link]

 CSS style directly change karta hai.


 Kab use karein? Jab element ka design JavaScript se change karna
ho.

[Link]("demo").[Link] = "red";

7. DOM Create / Add / Remove Elements


(a) [Link]("tag")

 Naya element banata hai.

let newP = [Link]("p");

(b) appendChild(child)

 Child ko parent ke andar last mein chipka deta hai.

[Link](newP);

(c) remove()

 Element delete karne ke liye.

[Link]("demo").remove();

(d) [Link](child)

 Parent ke andar se ek child hataata hai.

let parent = [Link]("container");


let child = [Link]("demo");
[Link](child);

8. DOM Events (user ke actions handle karna)


Common Events:

 onclick → click hone par


 onmouseover → mouse element par aane par
 onmouseout → mouse element se nikalne par
 onkeydown → key press hone par
 onkeyup → key chhodne par
 onsubmit → form submit hone par

Event Handling:
// Method 1: Inline
<button onclick="alert('Clicked!')">Click</button>

// Method 2: Property
[Link]("btn").onclick = function() {
alert("Clicked!");
};

// Method 3: addEventListener ✅ Best


[Link]("btn").addEventListener("click", function() {
alert("Clicked!");
});

9. DOM Traversal (elements ke beech


navigation)
 parentNode → parent element.
 children → list of child elements.
 firstChild / lastChild → first/last child.
 nextElementSibling → agla bhai.
 previousElementSibling → pichla bhai.

let next = [Link]("child1").nextElementSibling;

10. Useful DOM Properties


 [Link] → page title.
 [Link] → page ka URL.
 [Link] → <body> element.
 [Link] → saare forms.
 [Link] → element ka id.
 [Link] → element ki class.
✅ Final Easy Rule:
 Basic JavaScript (let, const, loops, conditions`) → logic banane ke
liye.
 DOM methods → HTML aur CSS ke elements ko
select/change/add/delete karne ke liye.
 Har website JavaScript + DOM dono ka combination hoti hai.

👉 Bhai, ab aap batao:


Kya aap chahte ho main ye notes table + examples ke saath ek HTML
practice file bana kar doon, jismein har cheez live test ho sake?

Bahut achha sawal kiya aapne Solaiman bhai 👌


Sirf notes padhne se banda expert nahi banta — asli power tab aati hai jab
hum common mistakes aur errors ko samajh kar unse bachna seekh lete
hain.

Chaliye main aapko DOM aur JavaScript use karte waqt programmers
ki sabse common problems bataata hoon, aur har ek ke solution bhi easy
words mein:

⚡ Common Mistakes & Problems


in DOM + JavaScript

1. Case-Sensitive galtiyaan
JavaScript case-sensitive hai.

 getElementById ✔
 getELementById ❌ (galat L capital)

👉 Ye error sabse zyada hota hai.

Solution: Hamesha correct spelling aur small/capital ka dhyaan rakho.


2. Element DOM mein exist hi nahi karta
Kabhi aap code mein likhte ho:

[Link]("demo").innerText = "Hello";

Lekin HTML mein <div id="demo"></div> hi nahi hai.

👉 Result: null return hoga aur error aayega → Cannot set property 'innerText' of
null.

Solution: Hamesha check karo ke jo id/class select kar rahe ho wo HTML


mein exist karta hai.

3. Script ka wrong position


Agar <script> tag ko <head> mein likh do bina defer attribute ke, tab
JavaScript DOM elements ko dhoondhne se pehle hi run kar jaata hai.

👉 Result: null ya error aayega.

Solution:

 Script ko hamesha </body> se pehle rakho,


 ya phir <script defer> use karo.

4. innerHTML vs innerText ka confusion


 innerHTML → HTML tags ko interpret karta hai.
 innerText → Sirf text ko set karta hai.

Programmers aksar innerHTML use karte hain jab sirf text chahiye hota hai
→ unwanted tags add ho jaate hain.

Solution:

 Text ke liye innerText ya textContent.


 HTML inject karna ho to innerHTML.
5. getElementsByClassName aur getElementsByTagName ko direct
use karna
Ye HTMLCollection return karte hain, jo array jaise hota hai but full array
nahi. Direct style change karne ki koshish karoge to error aayega.

[Link]("box").[Link] = "red"; // ❌ error

Solution: Loop use karo:

let boxes = [Link]("box");


for (let i = 0; i < [Link]; i++) {
boxes[i].[Link] = "red";
}

6. querySelector vs querySelectorAll ka galat


use
 querySelector sirf pehla match deta hai.
 querySelectorAll sab deta hai.

Programmers kabhi galti se querySelector use kar lete hain aur samajhte hain
ke sab select ho gaye.

7. const variables ko dubara assign karna


const pi = 3.14;
pi = 22/7; // ❌ error

👉 const ka value change nahi hota.

Solution: Agar value change karni hai to let use karo.

8. Events galat tarike se attach karna


Aksar log ek element ke multiple events attach karte hain aur overwrite ho
jaate hain.
[Link] = func1;
[Link] = func2; // pehla wala remove ho gaya

Solution: Always use addEventListener (multiple attach ho sakte hain).

[Link]("click", func1);
[Link]("click", func2);

9. =aur==aur===` ka confusion
 = → assignment (value dena)
 == → value compare (type ignore karta hai)
 === → value + type compare

let a = "5";
if (a == 5) // true
if (a === 5) // false

Programmers aksar galti se = use kar lete hain condition mein → bug.

10. Console ignore karna


Sabse badi galti → error aaya aur programmer ne console (Ctrl+Shift+I →
Console) nahi dekha.

👉 Console hamesha batata hai error kis line par hai.

✅ Summary (Easy Words)


 Spelling aur capital letters → hamesha check karo.
 Element HTML mein exist kare, script timing sahi ho.
 innerHTML vs innerText confusion avoid karo.
 Collections (class/tag) pe loop chalao.
 querySelector sirf pehle ko pakadta hai, querySelectorAll sabko.
 let vs const ko samajh kar use karo.
 Events ke liye addEventListener best hai.
 = aur === ka difference yaad rakho.
 Console use karna habit banao.
👉 Bhai, chaho to main in common mistakes ke examples ke saath ek
HTML+JS practice file bana dun jisme aap har galti aur uska solution live
dekh sako?

You might also like