# Manipulating the DOM Structure

createElement makes a new element, appendChild puts it inside a parent so it shows on the page, and remove takes an element off the page.

Apprentice · JavaScript · Updated November 15, 2026

By Gregor Dzierzon

## After this lesson you can

- Create an element with createElement, add it to the page with appendChild, and take one off with remove

## Key takeaways

- document.createElement makes a new element, but it isn't on the page yet.
- parent.appendChild(child) adds the new element as the parent's last child, and that puts it on the page.
- element.remove() takes an element, and everything inside it, off the page.

A to-do list that grows when you add a task, a cart that drops an item, a message that appears after you save: each one is an element created, added or removed while the page is open. This lesson shows how to make new elements, put them on the page, and take them off again.

## Creating New Elements

We use the document object to create a new element.

```javascript
// this creates a new div
const newDiv = document.createElement("div");
newDiv.textContent = "Free shipping on orders over $50";
```

Note that creating a new element does not add it to the DOM. We still need to insert it.

## Adding Elements to the DOM

```javascript
// creates the new div
const newDiv = document.createElement("div");
newDiv.textContent = "Free shipping on orders over $50";

// locate the parent element
const parent = document.getElementById("parent");

// insert the new div as a child of the parent element
parent.appendChild(newDiv);
```

`appendChild` adds the new element as the parent's last child. Now it's in the DOM, so it shows on the page.

## Removing Elements from the DOM

```javascript
// locate the element
const element = document.getElementById("target");
element.remove();
```

`remove()` takes the element off the page, along with everything inside it.

## Summary

The DOM is an in-memory representation of the HTML page. Each HTML element is an object in the DOM.

Using JavaScript, you can locate and manipulate any element in the DOM. This allows you to create a rich user experience by responding to user actions.

*Introducing the DOM* and *Locating DOM Elements*, earlier in this chapter, are there whenever you want to go over finding elements again, and the next chapter, on events, shows how to respond when people click, type and send a form.

## Do it: update the shopping cart

A shopper on the store's site just changed their cart: they added a notebook and took the socks out. The page still shows the old cart. Bring it up to date:

- Add `Spiral notebook, $4` to the cart, as its last item.
- Take the wool socks off the cart.
- Under the cart, in the messages area, add a paragraph that says `Cart updated.`

Practiced live on the lesson page: write the JavaScript right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

Changing a page's structure is three steps: make the element, fill it in, and put it where it belongs in the tree. Removing one is a single call.

## Key terms

- [createElement()](https://www.ask-codey.com/glossary/createelement)
- [appendChild()](https://www.ask-codey.com/glossary/appendchild)

## Cheat sheets

- [Manipulating the DOM Structure](https://www.ask-codey.com/cheat-sheets/working-with-elements-manipulating-the-dom-structure-cheat-sheet)

Source: https://www.ask-codey.com/lessons/working-with-elements-manipulating-the-dom-structure
