All posts
JavaScriptSeptember 6, 20267 min read

JavaScript DOM Manipulation for Beginners (Guide)

Learn JavaScript DOM manipulation for beginners: select elements, change text and styles, and handle clicks with simple, copy-paste code examples.

F
Fepiq Team
Fepiq

If you have ever clicked a button on a website and watched text change, a menu open, or an item get added to a list without the page reloading, you have seen JavaScript DOM manipulation in action. In short: the DOM (Document Object Model) is JavaScript's way of "seeing" and changing the HTML on a page, and DOM manipulation just means using JavaScript to select an element and then change it. This guide explains it from scratch, with small copy-pasteable examples, so you can start making your own web pages interactive today.

What is the DOM?

When a browser loads an HTML page, it does not just display the text of your file. It builds a live, in-memory model of the page called the Document Object Model, or DOM for short. Think of the DOM as a family tree: the <html> tag is the top, <body> is a child of it, and every element inside <body> (like <h1>, <p>, or <button>) is a branch on that tree. JavaScript can walk this tree, find any branch (element), and change its text, its style, or even add and remove branches entirely. That is the whole idea behind DOM manipulation.

Setting Up a Page to Practice On

You do not need any special tools. Create a file called index.html, paste in the code below, and open it in your browser. The <script> tag at the bottom is where all our JavaScript will go.

bash
<!DOCTYPE html>
<html>
  <body>
    <h1 id="greeting">Hello!</h1>
    <button id="changeBtn">Click me</button>

    <script>
      // our JavaScript will go here
    </script>
  </body>
</html>

This page has a heading with the id "greeting" and a button with the id "changeBtn". An id is a unique name you give an HTML element so JavaScript can find that exact element later. We will use these two ids throughout the guide.

How to Select an Element with JavaScript

Before you can change anything, you need to select it. The most common and beginner-friendly way is document.querySelector(), which takes a CSS-style selector (the same kind you use in stylesheets) and returns the first matching element.

js
const heading = document.querySelector("#greeting");
console.log(heading);

Line by line: document is a built-in object that represents the whole page. .querySelector("#greeting") searches the page for an element whose id is "greeting" (the # symbol means "id"). The result is stored in a variable called heading, so we can reuse it. console.log() just prints the element to the browser's developer console so you can check it worked.

  • document.querySelector("#id") — selects the first element with that id.
  • document.querySelector(".class") — selects the first element with that class (the . means "class").
  • document.querySelectorAll(".class") — selects every element with that class, as a list.
  • document.getElementById("id") — an older way to select by id, still very common.

How to Change Text on the Page

Once you have selected an element, changing its text is one line of code using the textContent property.

js
const heading = document.querySelector("#greeting");
heading.textContent = "Hello, JavaScript!";

heading.textContent = "..." replaces whatever text was inside the element with new text. There is also innerHTML, which works the same way but lets you insert actual HTML tags (like <strong>bold</strong>). Use textContent when you only need plain text, since it is simpler and safer.

How to Change Styles with JavaScript

Every element has a .style property that maps to CSS. You can change a style the same way you would change a variable.

js
const heading = document.querySelector("#greeting");
heading.style.color = "purple";
heading.style.fontSize = "32px";

heading.style.color sets the CSS color property. Notice fontSize is written in camelCase (fontSize, not font-size) because JavaScript property names cannot contain hyphens. This pattern — camelCase instead of hyphens — applies to every CSS property you touch through .style.

How to Respond to Clicks with Event Listeners

So far our changes ran immediately when the script loaded. To react to a user action, like a click, you attach an event listener with addEventListener().

js
const button = document.querySelector("#changeBtn");
const heading = document.querySelector("#greeting");

button.addEventListener("click", function () {
  heading.textContent = "You clicked the button!";
  heading.style.color = "green";
});

button.addEventListener("click", ...) tells the browser: "when this button is clicked, run this function." The function itself (the code between function () { ... }) only runs at the moment of the click, not when the page loads. Inside it, we reuse the same textContent and style tricks from earlier. This click-then-run pattern is the core of almost every interactive website feature.

How to Create and Add New Elements

You can also build brand-new HTML elements in JavaScript and insert them into the page. This is how apps add new list items, chat messages, or cards without reloading.

js
const list = document.querySelector("#todoList");

const item = document.createElement("li");
item.textContent = "Buy milk";

list.appendChild(item);

document.createElement("li") makes a brand-new <li> element that exists only in memory, not yet on the page. item.textContent gives it some text, just like before. list.appendChild(item) is the step that actually places it into the page, as the last child inside the element stored in list.

Putting It Together: A Tiny To-Do List

Here is a small, complete example that combines everything above: selecting elements, reading an input box's value, listening for clicks, and adding new elements to a list.

bash
<input id="taskInput" type="text" placeholder="New task" />
<button id="addBtn">Add</button>
<ul id="todoList"></ul>

<script>
  const input = document.querySelector("#taskInput");
  const addBtn = document.querySelector("#addBtn");
  const list = document.querySelector("#todoList");

  addBtn.addEventListener("click", function () {
    const item = document.createElement("li");
    item.textContent = input.value;
    list.appendChild(item);
    input.value = "";
  });
</script>

input.value reads whatever text the visitor typed into the text box. When the Add button is clicked, we create a new <li>, set its text to the input's value, append it to the list, and finally reset input.value back to an empty string so the box is ready for the next task. This is the same loop-and-build pattern used in real to-do apps, just without saving the data anywhere yet.

Common DOM Methods Cheat Sheet

Method or PropertyWhat It Does
document.querySelector(sel)Finds the first element matching a CSS selector
document.querySelectorAll(sel)Finds every matching element, as a list
element.textContentReads or sets an element's plain text
element.innerHTMLReads or sets an element's HTML content
element.style.propertyReads or sets one CSS style
element.addEventListener(event, fn)Runs fn whenever the given event happens
document.createElement(tag)Creates a new, unattached HTML element
parent.appendChild(child)Inserts an element as the last child of parent

Common Beginner Mistakes to Avoid

  • Running your script before the HTML exists — put your <script> tag at the end of <body>, or your selectors will return null.
  • Forgetting the # or . in querySelector — #id for ids, .class for classes, and neither for tag names like "button".
  • Confusing textContent with value — use value for form inputs, textContent for everything else.
  • Writing CSS property names with hyphens in JavaScript — use fontSize, not font-size.
  • Selecting an element once and expecting new elements added later to be included automatically — you usually need to select again or attach listeners as you create new elements.

Frequently asked questions

What is DOM manipulation in JavaScript?+

DOM manipulation means using JavaScript to select an HTML element on a page and then read or change it, such as updating its text, styling, or structure. It is how web pages become interactive instead of static.

What is the difference between the DOM and HTML?+

HTML is the text file the browser reads. The DOM is the live, in-memory tree structure the browser builds from that HTML while the page is open. JavaScript changes the DOM, and the browser instantly redraws the page to match, without needing to change the original HTML file.

Should I use querySelector or getElementById?+

For beginners, querySelector is a good default because it works with any CSS selector, including ids, classes, and tag names, using one consistent method. getElementById is slightly faster and very common in older code, but the difference rarely matters for typical websites.

Do I need a framework like React to manipulate the DOM?+

No. Plain JavaScript, sometimes called vanilla JavaScript, can select, change, create, and remove elements on its own, exactly as shown in this guide. Frameworks like React simply offer a more organized way to manage DOM updates once a page has many moving parts.

Why isn't my JavaScript finding my HTML element?+

The most common cause is that the script ran before the browser finished reading the HTML below it. Fix this by placing your <script> tag just before the closing </body> tag, so every element already exists when your JavaScript runs.

Comfortable with loops but want a refresher before combining them with the DOM? Review the fundamentals first.

Read JavaScript Loops for Beginners

DOM manipulation is the bridge between "knowing JavaScript syntax" and "building things people can actually click on." Start small: pick one button on a page you own, select it, and change something about it. Once selecting, changing text, changing styles, and listening for clicks feel natural, you already have the core skills used in every interactive website and, eventually, every JavaScript framework.

Want help turning your website idea into a fast, interactive product?

Talk to Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.