DOM & Events
HTML ko JavaScript se control karo โ DOM ek baar samajh lo, phir kabhi bhoologe nahi.
The DOM (Document Object Model) is a programming interface that the browser creates when it loads your HTML. It converts every tag into a tree of objects that JavaScript can read and modify.
Key DOM concepts:
// The entry point to everything
document // the root object
document.documentElement // <html> element
document.body // <body> element
document.head // <head> element
// Every tag becomes a "node"
// Element nodes: <div>, <p>, <span> etc.
// Text nodes: the text inside elements
// Comment nodes: <!-- comments -->
// Document node: the root itself
The DOM is not the HTML source code. It's a live representation. If JavaScript adds a new element, the DOM tree updates โ and so does what the user sees on screen.
Before you can change anything, you need to find the element. Three main methods:
getElementById when you have an ID. Use querySelector for everything else (it accepts any CSS selector). Use querySelectorAll when you need multiple elements.// 1. By ID โ fastest, returns ONE element or null
const header = document.getElementById('main-header');
// 2. By CSS selector โ returns FIRST match or null
const firstBtn = document.querySelector('.btn');
const deepItem = document.querySelector('nav ul li:first-child a');
// 3. All matches โ returns NodeList (NOT a real Array)
const allBtns = document.querySelectorAll('.btn');
const allParas = document.querySelectorAll('article > p');
querySelectorAll returns a NodeList, not an Array. It has .forEach() but NOT .map(), .filter(), or .reduce(). To use array methods: Array.from(nodes) or [...nodes].// This works โ NodeList has forEach
allBtns.forEach(btn => console.log(btn.textContent));
// This FAILS โ NodeList has no .map
allBtns.map(btn => btn.textContent); // TypeError!
// Fix: convert to real Array
[...allBtns].map(btn => btn.textContent); // works!Once you have an element reference, you can read and change its content, classes, and styles:
// TEXT CONTENT โ safe, no HTML parsing
el.textContent = 'Hello'; // sets text
el.textContent; // gets text (strips HTML tags)
// INNER HTML โ powerful but DANGEROUS
el.innerHTML = '<b>Bold</b>'; // renders as bold text
el.innerHTML; // gets HTML string
// CLASSES โ the modern way (no className string hacking)
el.classList.add('active');
el.classList.remove('hidden');
el.classList.toggle('open'); // add if missing, remove if present
el.classList.contains('active'); // returns true/false
// INLINE STYLES
el.style.color = 'red';
el.style.fontSize = '20px';
el.style.display = 'none';
// ATTRIBUTES
el.setAttribute('data-id', '42');
el.getAttribute('data-id'); // '42'
el.removeAttribute('data-id');
textContent sets plain text โ even if you write <b>Hi</b>, the user sees the raw tags. innerHTML parses and renders HTML โ but if the content comes from user input, it's an XSS vulnerability. Rule: use textContent for user data, innerHTML only for trusted content you control.You're not limited to modifying existing elements โ you can create new ones and remove old ones:
// CREATE a new element
const li = document.createElement('li');
li.textContent = 'New Item';
li.classList.add('item');
// ADD to the page
const ul = document.querySelector('ul');
ul.appendChild(li); // adds at the end
ul.insertBefore(li, ul.firstChild); // adds at the beginning
// REMOVE
li.remove(); // modern way โ removes itself
ul.removeChild(li); // old way โ parent removes child
container.innerHTML = newHTML is faster than removing one-by-one. But again โ never use it with untrusted user input. For dynamic content you build yourself, it's fine.// Fast way to replace all list items
const list = document.querySelector('ul');
list.innerHTML = items.map(item =>
`<li class="item">${item.name}</li>`
).join('');
DocumentFragment, append all elements to it, then append the fragment once โ only one reflow instead of 100.Events make the DOM interactive. The browser fires events when things happen โ clicks, key presses, form submissions, page load, etc.
// ADDING EVENT LISTENERS
const btn = document.querySelector('#myBtn');
btn.addEventListener('click', function(event) {
console.log('Clicked!');
console.log('Type:', event.type); // 'click'
console.log('Target:', event.target); // the element clicked
console.log('CurrentTarget:', event.currentTarget); // element with listener
});
// PREVENT DEFAULT BEHAVIOR
const link = document.querySelector('a');
link.addEventListener('click', function(e) {
e.preventDefault(); // stops link navigation
console.log('Link click intercepted!');
});
// STOP EVENT BUBBLING
child.addEventListener('click', function(e) {
e.stopPropagation(); // parent won't see this click
});
// REMOVE LISTENER (must pass same function reference)
function handleClick(e) { console.log('hi'); }
btn.addEventListener('click', handleClick);
btn.removeEventListener('click', handleClick);
<ul>. When any <li> is clicked, check event.target to see which one it was. This is faster, uses less memory, and automatically works for dynamically added elements.// Event delegation in action
const list = document.querySelector('ul');
list.addEventListener('click', function(e) {
const li = e.target.closest('li'); // find the <li> ancestor
if (!li) return; // click wasn't on a <li>
console.log('Clicked item:', li.textContent);
});
event.target = the element that was actually clicked (could be a child span inside a button). event.currentTarget = the element that has the addEventListener. In event delegation, these are different. Use .closest() to find the right ancestor.Lo kar liya โ Key Points:
- โ DOM = browser converts HTML into a live tree of JavaScript objects
- โ
getElementByIdfor IDs,querySelectorfor CSS selectors,querySelectorAllfor multiple - โ
querySelectorAllreturns NodeList โ NOT an Array, use[...nodes]to convert - โ
textContentfor safe text,innerHTMLonly for trusted content (XSS danger!) - โ
classList.add/remove/toggleis the modern way to handle CSS classes - โ
Event delegation = ONE listener on parent +
event.target.closest()to find the child - โ
event.targetโevent.currentTargetin delegation โ know the difference!
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login