๐Ÿ“… Week 6 - Day 3: DOM Manipulation

Friday | Duration: 3 hours

๐ŸŽฏ Learning Objectives

๐ŸŒณ What is the DOM?

DOM (Document Object Model) is a programming interface that represents your HTML document as a tree structure. JavaScript can access and change all elements, attributes, and styles.

๐ŸŽฏ Selecting Elements

getElementById()

// HTML
<h1 id="title">Hello World</h1>

// JavaScript
let titleElement = document.getElementById("title");
console.log(titleElement);  // <h1 id="title">Hello World</h1>

querySelector()

// Select by ID
let title = document.querySelector("#title");

// Select by class
let button = document.querySelector(".btn");

// Select by tag
let paragraph = document.querySelector("p");

querySelectorAll()

// Select ALL elements with class "card"
let cards = document.querySelectorAll(".card");
console.log(cards.length);  // Number of cards

โœ๏ธ Changing HTML Content

textContent

// HTML
<p id="demo">Original text</p>

// JavaScript
let demo = document.getElementById("demo");
demo.textContent = "New text!";
// Result: <p id="demo">New text!</p>

innerHTML

// Can include HTML tags
let container = document.getElementById("container");
container.innerHTML = "<h2>Hello</h2><p>Welcome!</p>";

๐ŸŽจ Changing CSS Styles

let box = document.getElementById("box");

// Change individual styles
box.style.color = "red";
box.style.backgroundColor = "yellow";
box.style.fontSize = "24px";
box.style.padding = "20px";
box.style.border = "2px solid black";

โž• Adding/Removing Classes

let element = document.getElementById("myElement");

// Add class
element.classList.add("active");

// Remove class
element.classList.remove("hidden");

// Toggle class (add if not present, remove if present)
element.classList.toggle("visible");

๐Ÿ”˜ Changing Attributes

let image = document.querySelector("img");

// Change src attribute
image.src = "newimage.jpg";

// Change alt text
image.alt = "New description";

// Add/change href
let link = document.querySelector("a");
link.href = "https://google.com";

๐Ÿ’ป Complete Interactive Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DOM Manipulation Demo</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            padding: 40px;
            background: #f0f0f0;
        }
        .box {
            width: 200px;
            height: 200px;
            background: lightblue;
            margin: 20px;
            padding: 20px;
            border-radius: 10px;
        }
        .highlight {
            background: yellow;
            font-weight: bold;
        }
        button {
            padding: 10px 20px;
            margin: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1 id="heading">DOM Manipulation Practice</h1>
    <p id="message">This is the original message</p>
    
    <div id="box" class="box">
        <p>This is a box</p>
    </div>

    <button onclick="changeText()">Change Text</button>
    <button onclick="changeColor()">Change Color</button>
    <button onclick="toggleHighlight()">Toggle Highlight</button>
    <button onclick="addContent()">Add Content</button>

    <div id="output"></div>

    <script>
        // Change text content
        function changeText() {
            let heading = document.getElementById("heading");
            heading.textContent = "Text Changed!";
            
            let message = document.getElementById("message");
            message.textContent = "The message has been updated";
        }

        // Change CSS styles
        function changeColor() {
            let box = document.getElementById("box");
            box.style.backgroundColor = "lightcoral";
            box.style.color = "white";
            box.style.fontSize = "20px";
        }

        // Toggle CSS class
        function toggleHighlight() {
            let message = document.getElementById("message");
            message.classList.toggle("highlight");
        }

        // Add new HTML content
        function addContent() {
            let output = document.getElementById("output");
            output.innerHTML += "<p>New paragraph added!</p>";
        }
    </script>
</body>
</html>

๐Ÿงช Live Demo on This Page

Click the button below!

Original Box

โœ๏ธ Practice Exercises

  1. Create a page with a heading - add button to change the heading text
  2. Create a div with background color - add button to change the color
  3. Create a paragraph - add button to toggle between two different texts
  4. Create an image - add button to change the image source
  5. Create multiple elements - add button to hide/show them

๐ŸŽฏ Mini Project: Profile Card Updater

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Profile Card Updater</title>
    <style>
        .profile-card {
            width: 300px;
            padding: 30px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
            text-align: center;
        }
        .profile-card img {
            width: 100px;
            height: 100px;
            border-radius: 50%;
        }
        button { margin: 5px; padding: 10px; cursor: pointer; }
    </style>
</head>
<body>
    <div class="profile-card">
        <img id="profilePic" src="avatar1.jpg" alt="Profile">
        <h2 id="profileName">John Doe</h2>
        <p id="profileRole">Web Developer</p>
        <p id="profileBio">Learning JavaScript</p>
    </div>

    <button onclick="updateProfile()">Update Profile</button>
    <button onclick="changeTheme()">Change Theme</button>

    <script>
        function updateProfile() {
            document.getElementById("profileName").textContent = "Ahmad Garba";
            document.getElementById("profileRole").textContent = "Frontend Developer";
            document.getElementById("profileBio").textContent = "Expert in HTML, CSS & JavaScript";
        }

        function changeTheme() {
            let card = document.querySelector(".profile-card");
            card.style.background = "linear-gradient(135deg, #667eea 0%, #764ba2 100%)";
            card.style.color = "white";
        }
    </script>
</body>
</html>

โœ… Today's Deliverable

โ† Previous Course Home Next: Week 7 โ†’