DEV Community

Cover image for Understanding the DOM: A Practical Guide for Beginners
Emmanuel Olusegun
Emmanuel Olusegun

Posted on

Understanding the DOM: A Practical Guide for Beginners

Hey there, future JavaScript wizard!
If you’ve ever stared at a webpage and thought, "How does JavaScript actually change stuff on the screen?"—welcome! You're about to unlock one of the most magical parts of web development: The DOM.

By the end of this post, you’ll know how to:

  1. Grab elements from a webpage like a pro
  2. Change text, colors, and even delete stuff(responsibly)
  3. Make buttons do cool things when clicked
  4. Feel like a JavaScript sorcerer _Let's jump in! _

What the Heck is the DOM?
Think of the DOM (Document Object Model) as a blueprint of your webpage. It's like a family tree, but instead of relatives, it has HTML elements.

For example, if your webpage is:

  <body>
    <h1>Hello, DOM!</h1>
    <button>Click me!</button>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

JavaScript sees it like this:

📁 document (the entire page)
┣ 📂 html (the root)
┃ ┣ 📂 body
┃ ┃ ┣ 📄 h1 (Hello, DOM!)
┃ ┃ ┗ 📄 button (Click me!)

Each of these elements is a node that JavaScript can find, change, or remove. Now let’s learn how to do just that!

Finding Elements in the DOM
Before we can do cool things, we need to grab elements. Here’s how:

1. Get an element by ID

let title = document.getElementById("main-title");
console.log(title.innerText); //Logs text inside the element
Enter fullscreen mode Exit fullscreen mode

2. Get an element by class

let button = document.querySelector(".btn"); //Selects the first button with class "btn"
Enter fullscreen mode Exit fullscreen mode

3. Get all elements of a type (like all <li> tags)

let listItems = document.querySelectorAll("li");
console.log(listItems.length); //Counts all <li> elements
Enter fullscreen mode Exit fullscreen mode

Think of these like treasure maps: They help JavaScript find the exact thing you want to change.

Changing Stuff in the DOM
Okay, we’ve found the elements—now let's mess with them.

1. Change the text inside an element

let heading = document.getElementById("main-title");
heading.innerText = "DOM Magic Activated!"; //Boom! Text changed.
Enter fullscreen mode Exit fullscreen mode

2. Change styles dynamically

heading.style.color = "blue"; // Changes text color to blue
heading.style.fontSize = "30px"; // Makes it bigger
Enter fullscreen mode Exit fullscreen mode

3. Change attributes (like links)

let link = document.querySelector("a");
link.setAttribute("href", "https://dev.to"); // Updates the link destination
Enter fullscreen mode Exit fullscreen mode

Fun experiment: Try changing the backgroundColor of the body to "black" and see what happens!

Creating & Removing Elements

1. Add a new element (like a paragraph)

let newParagraph = document.createElement("p");
newParagraph.innerText = "JavaScript just created me!";
document.body.appendChild(newParagraph); // Adds it to the page
Enter fullscreen mode Exit fullscreen mode

2. Remove an element (like deleting a button)

let unwantedButton = document.querySelector(".btn");
unwantedButton.remove(); // Poof! It's gone.
Enter fullscreen mode Exit fullscreen mode

Making Things Interactive (Event Listeners)
Want to make a button actually do something? Add an event listener!

let btn = document.querySelector(".btn");
btn.addEventListener("click", function () {
  alert("You clicked me!");
});
Enter fullscreen mode Exit fullscreen mode

Try this: Instead of an alert, change the background color when clicked.

Conclusion
Congratulations, you’ve just taken your first big step into the world of DOM manipulation!

What Next:
🔹 Try making a simple to-do list using what you just learned.
🔹 Experiment with changing different properties dynamically.
🔹 Keep building, breaking, and experimenting—that's how you grow as a developer!

If you found this useful, drop your questions and thought in the comments and let’s keep the DOM magic alive!
Happy coding!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay