2.5 JavaScript and the DOM – Interacting with the Browser (Made Easy!)

Table of Contents

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

Family Tree Diagram Showing Dom Structure – Html &Gt; Head &Amp; Body &Gt; Elements
2.5 Javascript And The Dom – Interacting With The Browser (Made Easy!) 6


πŸ”Ž 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

2.5 Javascript And The Dom – Interacting With The Browser (Made Easy!) 7

✍️ 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.


Innertext Vs Innerhtml Info 1
2.5 Javascript And The Dom – Interacting With The Browser (Made Easy!) 8


🎨 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";

Before And After Style Change: Text Turns Blue With Yellow Background
2.5 Javascript And The Dom – Interacting With The Browser (Made Easy!) 9


🧱 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. πŸš€


1 Nj67Vyyys91V5 D2Smqega
2.5 Javascript And The Dom – Interacting With The Browser (Made Easy!) 10


🧠 Key Concepts Recap

ConceptWhat It DoesExample
getElementById()Finds an element by IDGrab a <p> with ID “greeting”
querySelector()Finds the first match using CSS selectorFirst .message paragraph
querySelectorAll()Finds all matches using CSS selectorAll <li> items
innerHTMLChanges HTML inside an elementAdd <strong> tags
textContentChanges text onlyChange text to “Welcome”
valueSets value of form elementsFill in a text box
.styleChanges CSS styles dynamicallyMake text blue
appendChild()Adds a new elementAdd a new <li>
removeChild()Removes a child elementRemove first <li>
insertBefore()Inserts a new element before anotherAdd 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 πŸš€πŸ’»!

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts