Day11: JavaScript DOM Manipulation: Selecting & Modifying Elements 🚀


Introduction
JavaScript is a powerful language that enables developers to interact with web pages dynamically. One of its key capabilities is DOM (Document Object Model) Manipulation, which allows us to select, modify, and update elements on a webpage. Understanding how to manipulate the DOM efficiently is crucial for building interactive and user-friendly websites.
In this post, we’ll explore how to select and modify HTML elements using JavaScript, along with real-life code examples to enhance your understanding.
🏆 Selecting Elements in the DOM
Before we modify any element, we first need to select it. JavaScript provides several ways to do this:
1️⃣ getElementById()
- Select by ID
// Select an element with the ID "title"
let titleElement = document.getElementById("title");
console.log(titleElement.innerText); // Outputs the text inside the element
Use Case: Useful when you need to target a unique element, such as a header, footer, or main container.
2️⃣ getElementsByClassName()
- Select by Class
// Select all elements with class "item"
let items = document.getElementsByClassName("item");
console.log(items[0].innerText); // Access the first element in the collection
Use Case: When multiple elements share the same class, such as list items, buttons, or sections.
3️⃣ getElementsByTagName()
- Select by Tag Name
// Select all <p> elements
let paragraphs = document.getElementsByTagName("p");
console.log(paragraphs.length); // Count of <p> elements on the page
Use Case: Best for selecting all paragraphs, divs, or images on a webpage.
4️⃣ querySelector()
- Select First Matching Element
// Select the first element that matches the CSS selector
let firstButton = document.querySelector(".btn");
console.log(firstButton.innerText);
Use Case: When you need to target the first occurrence of an element with a specific class, ID, or attribute.
5️⃣ querySelectorAll()
- Select All Matching Elements
// Select all buttons on the page
let allButtons = document.querySelectorAll("button");
console.log(allButtons.length); // Count of all <button> elements
Use Case: Useful when handling multiple elements, such as all checkboxes, input fields, or links.
✍️ Modifying Elements in the DOM
Once we’ve selected an element, we can modify its content, attributes, and styles dynamically.
1️⃣ Changing Text Content (innerText
& textContent
)
let heading = document.getElementById("title");
heading.innerText = "Updated Title!"; // Changes the text inside the element
Use Case: When you need to update headings, labels, or descriptions dynamically.
2️⃣ Changing HTML Content (innerHTML
)
let divContainer = document.getElementById("container");
divContainer.innerHTML = "<p>New Paragraph Added!</p>";
Use Case: Adding dynamic content, such as a notification or a new section.
3️⃣ Modifying Attributes (setAttribute
& getAttribute
)
let link = document.querySelector("a");
link.setAttribute("href", "https://example.com"); // Updates the link URL
console.log(link.getAttribute("href")); // Retrieves the updated value
Use Case: Updating image sources, links, and form input attributes dynamically.
4️⃣ Changing CSS Styles (style
property)
let button = document.querySelector(".btn");
button.style.backgroundColor = "blue"; // Change background color
button.style.fontSize = "18px"; // Increase font size
Use Case: Used for dynamic theming, animations, and UI modifications.
5️⃣ Adding & Removing CSS Classes (classList
)
let card = document.querySelector(".card");
card.classList.add("highlight"); // Adds a new CSS class
card.classList.remove("hidden"); // Removes an existing class
Use Case: Useful for toggling dark mode, validation states, or animations.
🔄 Real-Life Example: Interactive To-Do List
Here’s how we can add items dynamically to a list using DOM Manipulation:
HTML:
<input type="text" id="todoInput" placeholder="Enter a task">
<button id="addTask">Add Task</button>
<ul id="taskList"></ul>
JavaScript:
document.getElementById("addTask").addEventListener("click", function() {
let taskText = document.getElementById("todoInput").value;
if (taskText !== "") {
let newTask = document.createElement("li");
newTask.innerText = taskText;
document.getElementById("taskList").appendChild(newTask);
document.getElementById("todoInput").value = ""; // Clear input field
}
});
How It Works:
✔️ Retrieves input value from the text field.
✔️ Creates a new <li>
element dynamically.
✔️ Appends the new task to the <ul>
list.
✔️ Clears the input field after adding a task.
🎯 Conclusion
Mastering DOM Manipulation in JavaScript is essential for creating dynamic, interactive web pages. By using methods like getElementById()
, querySelector()
, and classList.add()
, you can select and modify elements effectively.
🚀 Key Takeaways: ✔️ Use querySelector()
and querySelectorAll()
for modern and flexible selections.
✔️ Modify content using innerText
, innerHTML
, and attributes using setAttribute()
.
✔️ Change styles dynamically with style
and classList
.
✔️ Implement real-world examples like dynamic forms and interactive lists.
Now that you know how to manipulate the DOM, go ahead and build something amazing! 🎨✨
💬 What’s the most interesting way you’ve used DOM Manipulation in a project? Drop a comment below! 🚀
Subscribe to my newsletter
Read articles from Lokesh Prajapati directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Lokesh Prajapati
Lokesh Prajapati
🚀 JavaScript | React | Shopify Developer | Tech Blogger Hi, I’m Lokesh Prajapati, a passionate web developer and content creator. I love simplifying JavaScript, React, and Shopify development through easy-to-understand tutorials and real-world examples. I’m currently running a JavaScript Basics to Advanced series on Medium & Hashnode, helping developers of all levels enhance their coding skills. My goal is to make programming more accessible and practical for everyone. Follow me for daily coding tips, tricks, and insights! Let’s learn and grow together. 💡🚀 #JavaScript #React #Shopify #WebDevelopment #Coding #TechBlogger #LearnToCode