BCA Student Stories & UpdatesLive
DOM Selection: querySelector, textContent, innerHTML & Dynamic Styles
The Document Object Model (DOM) is an object-oriented tree representation of the webpage. Through the DOM API, JavaScript can inspect, add, remove, and alter HTML tags, attributes, and CSS styling in real-time without reloading the page.
Day 28 Step-by-Step Practical Flow
🎯1. What You Will Learn Today
DOM Selection: querySelector, textContent, innerHTML & Dynamic Styles — Comprehensive Practical Guide
Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.
The Document Object Model (DOM) is an object-oriented tree representation of the webpage. Through the DOM API, JavaScript can inspect, add, remove, and alter HTML tags, attributes, and CSS styling in real-time without reloading the page.
"HTML is the static blueprint drawing of a house; the DOM is the physical constructed building with electrical wiring; JavaScript is the resident who can flip light switches, paint walls, and rearrange furniture inside."
👨🏫3. Teacher Demonstration Code
See how querySelector targets HTML elements and updates text and background color on the fly.
<!DOCTYPE html>
<html>
<head>
<style>
.status-card {
padding: 16px;
border-radius: 8px;
background: #f1f5f9;
font-family: sans-serif;
}
.active-badge {
color: #16a34a;
font-weight: bold;
}
</style>
</head>
<body>
<div class="status-card">
<h3 id="student-header">Student Record</h3>
<p>Status: <span id="status-pill">Pending</span></p>
</div>
<script>
// 1. Select elements
const header = document.querySelector("#student-header");
const pill = document.querySelector("#status-pill");
// 2. Update content and styles
header.textContent = "Aarav Sharma (BCA 1st Sem)";
pill.textContent = "Verified & Active";
pill.classList.add("active-badge");
</script>
</body>
</html>2. Hands-On Practical Lab: Follow Instructions in VS Code
Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.
Day 28 Practical Task Test Studio
Select #portal-title with querySelector and update textContent to "Nextsem BCA Hub".
Select #badge, update textContent to "Active", and set style.color to "#16a34a".
Spot the Bug: Script Executing Before DOM Element Exists
Tap or click on the exact line below that contains the error:
Click any code line<head> <script> // Script runs before <body> is parsed! const heading = document.querySelector("#my-heading"); heading.textContent = "New Title"; // Throws TypeError: Cannot set properties of null </script></head><body> <h1 id="my-heading">Original Title</h1></body>Senior Developer Forensic Analysis:
The browser executes <script> in <head> immediately before the <body> HTML is parsed, so document.querySelector("#my-heading") returns null.
📝Independent Practice: Assignment
📄 announcement-banner.htmlInteractive University Campus Announcement Banner
- •Create day-28/index.html with an announcement card component.
- •Use document.querySelector() to target card title, date, and priority tag.
- •Write JavaScript that updates the text and applies dynamic styling based on priority.
- •Verify in the browser that all DOM updates apply cleanly on initial page load.
Open Visual Studio Code on your PC (code .), create announcement-banner.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BCA Day 28 - DOM Selection & Dynamic Styling</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Segoe UI', Tahoma, sans-serif;
background: #0b1120;
color: white;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.wrapper { max-width: 500px; width: 100%; }
.announcement-card {
background: #1e293b;
border: 1px solid #334155;
border-radius: 16px;
padding: 24px;
transition: all 0.3s ease;
}
.priority-badge {
display: inline-block;
padding: 4px 10px;
border-radius: 50px;
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
margin-bottom: 12px;
}
.card-title { font-size: 18px; margin-bottom: 8px; }
.card-date { font-size: 12px; color: #94a3b8; margin-bottom: 12px; }
.card-body { font-size: 13px; color: #cbd5e1; line-height: 1.5; }
/* Dynamic Priority Themes applied via DOM */
.priority-high {
border-left: 5px solid #ef4444;
background: #1c1917;
}
.priority-high .priority-badge { background: #ef4444; color: white; }
</style>
</head>
<body>
<div class="wrapper">
<div class="announcement-card" id="campusCard">
<span class="priority-badge" id="priorityTag">Normal</span>
<h2 class="card-title" id="cardTitle">Loading Announcement...</h2>
<div class="card-date" id="cardDate">--</div>
<p class="card-body" id="cardBody">Fetching campus records...</p>
</div>
</div>
<script>
// 1. Select DOM elements using document.querySelector
const card = document.querySelector("#campusCard");
const priorityTag = document.querySelector("#priorityTag");
const cardTitle = document.querySelector("#cardTitle");
const cardDate = document.querySelector("#cardDate");
const cardBody = document.querySelector("#cardBody");
// 2. Data payload
const notice = {
title: "BCA 1st Semester Term-End Practical Exam Schedule",
date: "October 2026 Notification",
body: "All registered BCA students must push their verified GitHub repositories for Days 1–30 prior to the final laboratory viva examination.",
priority: "high"
};
// 3. Update DOM content dynamically
cardTitle.textContent = notice.title;
cardDate.textContent = notice.date;
cardBody.textContent = notice.body;
priorityTag.textContent = notice.priority.toUpperCase() + " PRIORITY";
// 4. Apply dynamic style classes
if (notice.priority === "high") {
card.classList.add("priority-high");
}
</script>
</body>
</html>🎓4. Viva Check: Mandatory Conceptual Test
Why is textContent preferred over innerHTML when inserting user-generated text into a webpage?
5. Mandatory Step: Push to GitHub to Unlock Next Lesson
Target Repo: html-css-projects
Exact GitHub Setup Guide & Expected Structure
Day 28 RequirementsFollow these exact folder and file names to prevent verification errors.
index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.day-28/ for today so you don't overwrite previous days.main.You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 28 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 27: JavaScript Objects, Nested Data & JSON Serialization Basics first.
Complete Day 27 First