# DOM Manipulation

> Learn how to dynamically alter the structure, style, and content of a web page using JavaScript.

Subject: JavaScript | Lesson 42 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/dom-manipulation
Previous: [The DOM: Selecting & Traversing](https://codewithanimation.in/javascript/dom-selection-and-traversal.md) | Next: [Events & Listeners](https://codewithanimation.in/javascript/events-and-listeners.md)

## Changing the Page

Once you have selected a DOM node, you can modify almost anything about it.

You can change its text, alter its CSS classes, update its HTML attributes (like `disabled` or `src`), or even completely delete it from the page.

## The Virtual Paintbrush

Here are the most common ways to manipulate the DOM:

**Content**: Use `node.textContent = 'Hello'` to safely change text. Use `node.innerHTML = '<b>Hi</b>'` to inject HTML tags.

**Classes**: Use `node.classList.add('active')`, `node.classList.remove()`, or `node.classList.toggle()` to manage CSS classes.

**Attributes**: Use `node.setAttribute('disabled', 'true')` or `node.removeAttribute('disabled')`.

## Creating & Destroying

You aren't limited to modifying existing elements. You can create entirely new branches of the tree.

To add a new element: First, create it in memory using `const newDiv = document.createElement('div')`. Second, inject it into the live DOM using `parentElement.appendChild(newDiv)`.

To destroy an element: Simply call `node.remove()`.

## Common mistake: The Security Risk (XSS)

Never use `innerHTML` to display text typed by a user.

If a user types `<script>stealCookies()</script>` and you inject it using `innerHTML`, the browser will execute their malicious code. This is called a Cross-Site Scripting (XSS) attack. Always use `textContent` when dealing with user input, as it safely escapes HTML tags.

## Tip: The .style Object

You can directly modify inline CSS styles using `node.style`. Remember that CSS properties with hyphens become camelCase in JS: `node.style.backgroundColor = 'blue'` instead of `background-color`.

## Try it: The DOM Workshop

Use the control panel to execute JavaScript manipulation commands against the live Preview Card below.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/dom-manipulation

## Check yourself

### Question 1

Why is `textContent` preferred over `innerHTML` when displaying user input?

- A. Because textContent safely treats everything as a literal string, preventing XSS attacks.
- B. Because textContent renders faster.
- C. Because innerHTML doesn't support modern HTML5 tags.

Correct answer: A

Using `innerHTML` with untrusted user data allows attackers to inject malicious `<script>` tags into your site.

- Why not B: Incorrect. While technically slightly faster, security is the primary reason.
- Why not C: Incorrect. innerHTML supports all tags.

### Question 2

How do you toggle a CSS class named 'hidden' on an element?

- A. element.classList.toggle('hidden')
- B. element.className = 'hidden'
- C. element.style.hidden = true

Correct answer: A

The `classList` API is the safest and easiest way to manage classes without accidentally deleting other existing classes.

- Why not B: Incorrect. That would overwrite ALL existing classes on the element.
- Why not C: Incorrect. 'hidden' is a class, not an inline CSS style property.

### Question 3

What happens immediately after you run `const div = document.createElement('div')`?

- A. The div is created in memory, but is NOT yet visible on the page.
- B. The div appears at the bottom of the page.
- C. The div replaces the entire body.

Correct answer: A

Creating an element only creates it in the JavaScript engine's memory. You must explicitly attach it to the live DOM tree using a method like `appendChild()`.

- Why not B: Incorrect. The browser doesn't know where you want to put it yet.
- Why not C: Incorrect.

## Remember this

- Use `textContent` to safely change text; avoid `innerHTML` for user input.
- Use `classList` to `add()`, `remove()`, or `toggle()` CSS classes.
- Use `setAttribute()` to modify HTML attributes.
- `document.createElement()` makes an element in memory; you must `append()` it to make it visible.
- Call `.remove()` on a node to destroy it.
