DEV Community

Cover image for DOM Manipulation in JavaScript โ€“ Step by Step Guide
Manikanta Yarramsetti
Manikanta Yarramsetti

Posted on

DOM Manipulation in JavaScript โ€“ Step by Step Guide

๐Ÿš€ DOM Manipulation in JavaScript โ€“ Step by Step Guide

If youโ€™re learning JavaScript, DOM Manipulation is one of the most important skills you must understand.

In this article, weโ€™ll learn:

  • What the DOM is
  • How to select elements
  • How to change content and styles
  • How to handle events
  • How to create and remove elements

Everything with simple examples ๐Ÿ‘‡


๐Ÿง  What is the DOM?

DOM stands for Document Object Model.

When a web page loads, the browser converts HTML into a tree-like structure that JavaScript can access and modify.

JavaScript uses the DOM to read, change, add, or remove elements on a web page.


๐Ÿงฉ Sample HTML

<!DOCTYPE html>
<html>
<head>
  <title>DOM Manipulation</title>
</head>
<body>

  <h1 id="title">Hello DOM</h1>
  <p class="description">Learning DOM manipulation</p>

  <button id="btn">Click Me</button>

  <script src="script.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Step 1: Selecting DOM Elements

1๏ธโƒฃ Select by ID

const title = document.getElementById("title");
console.log(title);
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ Select by Class

const desc = document.getElementsByClassName("description");
console.log(desc[0]);
Enter fullscreen mode Exit fullscreen mode

3๏ธโƒฃ Select using CSS Selectors (Recommended)

const heading = document.querySelector("#title");
const paragraph = document.querySelector(".description");
Enter fullscreen mode Exit fullscreen mode

โœ๏ธ Step 2: Changing Text Content

You can change the text inside elements using textContent or innerHTML.

heading.textContent = "DOM is Awesome!";
heading.innerHTML = "<span>DOM is Powerful!</span>";
Enter fullscreen mode Exit fullscreen mode

๐ŸŽจ Step 3: Changing Styles

You can also change the style of elements dynamically.

heading.style.color = "blue";
heading.style.fontSize = "32px";
Enter fullscreen mode Exit fullscreen mode

Or use CSS classes:

.highlight {
  color: red;
  background: yellow;
}

heading.classList.add("highlight");
Enter fullscreen mode Exit fullscreen mode

๐Ÿ–ฑ๏ธ Step 4: Handling Events

You can respond to user actions like clicks.

const button = document.querySelector("#btn");

button.addEventListener("click", () => {
  alert("Button clicked!");
  heading.textContent = "You clicked the button!";
});
Enter fullscreen mode Exit fullscreen mode

โž• Step 5: Creating New Elements

Create and insert new elements into the DOM.

const newParagraph = document.createElement("p");
newParagraph.textContent = "This paragraph was created using JavaScript";

document.body.appendChild(newParagraph);
Enter fullscreen mode Exit fullscreen mode

โŒ Step 6: Removing Elements

Remove elements from the DOM when needed.

newParagraph.remove();
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Step 7: Full Mini Example

Hereโ€™s a full working example:

button.addEventListener("click", () => {
  const item = document.createElement("li");
  item.textContent = "New Item Added";
  document.body.appendChild(item);
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)