Hey awesome people! π Iβm Vikas Sankhla, a Full Stack Developer and the founder of the Web Codder YouTube channel. Today, weβre going to learn how JavaScript talks to your webpage.
This is SUPER IMPORTANT because it’s how your code:
β
Changes text
β
Adds buttons
β
Moves things around
β
Changes colors and styles
Sounds cool? Letβs go! π
π§ What Is the DOM (Document Object Model)?
Imagine your webpage is a big family tree π³.
- Each HTML tag (like
<p>
,<div>
,<button>
) is a family member. - The whole tree is called the DOM.
So, the DOM is JavaScriptβs way of “seeing” your page. It lets you access and change anything on the page!
Example:
htmlCopyEdit<h1>Hello World</h1>
<button>Click Me!</button>
π The DOM knows:
- Thereβs an
<h1>
with the text “Hello World” - Thereβs a
<button>
that says “Click Me!”

π Accessing HTML Elements
We can grab elements from the DOM using some handy tools. Here are the top ones youβll use:
1οΈβ£ document.getElementById()
You use this when an element has an ID.
Example HTML:
htmlCopyEdit<p id="greeting">Hi there!</p>
JavaScript:
jsCopyEditlet myPara = document.getElementById("greeting");
console.log(myPara); // Shows: <p id="greeting">Hi there!</p>
Super useful if you know the exact ID! β
2οΈβ£ document.querySelector()
This grabs the first match of a CSS selector.
Example:
htmlCopyEdit<p class="message">Hello!</p>
JavaScript:
jsCopyEditlet msg = document.querySelector(".message");
console.log(msg); // <p class="message">Hello!</p>
You can use IDs, classes, tags… anything CSS-like!
3οΈβ£ document.querySelectorAll()
This grabs ALL elements that match.
Example:
htmlCopyEdit<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
JavaScript:
jsCopyEditlet items = document.querySelectorAll("li");
console.log(items); // NodeList of all <li> elements

βοΈ Changing Content
Want to change what something says? JavaScript makes it easy.
1οΈβ£ innerHTML
This changes the HTML inside an element.
jsCopyEditdocument.getElementById("greeting").innerHTML = "<strong>Welcome!</strong>";
βοΈ It can add tags like <strong>
.
2οΈβ£ textContent
This changes the text only (no HTML).
jsCopyEditdocument.getElementById("greeting").textContent = "Welcome!";
βοΈ Great if you just want plain text.
3οΈβ£ value
This is for form elements like <input>
or <textarea>
.
Example:
htmlCopyEdit<input type="text" id="nameBox">
JavaScript:
jsCopyEditdocument.getElementById("nameBox").value = "Vikas";
βοΈ It fills in the text box.

π¨ Changing Styles
Want to change colors, fonts, or sizes? Easy! π
Using .style
Example:
jsCopyEditlet myPara = document.getElementById("greeting");
myPara.style.color = "blue";
myPara.style.fontSize = "24px";
π You can change any CSS property using JavaScript.
Important: CSS properties with dashes (like background-color
) become camelCase (backgroundColor
).
Example:
jsCopyEditmyPara.style.backgroundColor = "yellow";

π§± Adding and Removing Elements
Sometimes you want to add new stuff or remove things.
Hereβs how:
1οΈβ£ appendChild()
This adds a new element to the end.
Example:
jsCopyEditlet newItem = document.createElement("li");
newItem.textContent = "New Item";
document.getElementById("myList").appendChild(newItem);
βοΈ Boom π₯ β new list item added!
2οΈβ£ removeChild()
This removes a child element.
Example:
jsCopyEditlet list = document.getElementById("myList");
let firstItem = list.firstElementChild;
list.removeChild(firstItem);
βοΈ First list item is gone! ποΈ
3οΈβ£ insertBefore()
This inserts a new element BEFORE another.
Example:
jsCopyEditlet newItem = document.createElement("li");
newItem.textContent = "Iβm first!";
let list = document.getElementById("myList");
let firstItem = list.firstElementChild;
list.insertBefore(newItem, firstItem);
βοΈ New item slides in at the top. π

π§ Key Concepts Recap
Concept | What It Does | Example |
---|---|---|
getElementById() | Finds an element by ID | Grab a <p> with ID “greeting” |
querySelector() | Finds the first match using CSS selector | First .message paragraph |
querySelectorAll() | Finds all matches using CSS selector | All <li> items |
innerHTML | Changes HTML inside an element | Add <strong> tags |
textContent | Changes text only | Change text to “Welcome” |
value | Sets value of form elements | Fill in a text box |
.style | Changes CSS styles dynamically | Make text blue |
appendChild() | Adds a new element | Add a new <li> |
removeChild() | Removes a child element | Remove first <li> |
insertBefore() | Inserts a new element before another | Add new <li> at the top |
π― Conclusion
The DOM is like your playground β with JavaScript, you can build, change, and customize your webpage however you like! π οΈπ
We covered:
- How the DOM works
- How to grab elements
- How to change content & styles
- How to add or remove elements dynamically
π Keep practicing, and soon you’ll be DOM master! πͺ
For more awesome coding tutorials:
π₯ Subscribe to my YouTube: https://www.youtube.com/@web_codder/?sub_confirmation=1
πΈ Follow us on Instagram: https://www.instagram.com/web_codder_official/
π¬ Join our WhatsApp community: https://webcodder.dev/whatsapp
Letβs keep learning and growing together ππ»!