DEV Community

Ivan Kaminskyi
Ivan Kaminskyi

Posted on • Originally published at jsjungle.dev

How to Modify DOM Elements with JavaScript: A Comprehensive Guide

Getting Started

Before we dive into manipulating the Document Object Model (DOM) with JavaScript, let's ensure we have the right setup. You'll need a modern web browser like Google Chrome, Mozilla Firefox, or Safari, and a text editor such as Sublime Text, Visual Studio Code, or Atom. If you have these tools installed, you're all set to begin.

JavaScript is an interpreted language, which means it's executed directly in the browser, and you don't need any special tools to compile it. To add JavaScript to a web page, you just need to insert it between <script> tags in your HTML document.

<!DOCTYPE html>
<html>
<head>
    <title>My first JavaScript web page</title>
</head>
<body>
    <script>
        alert("Hello, World!");
    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this example, we used a simple JavaScript command to display an alert box with the message "Hello, World!". Now, let's get to the core topic: The DOM.

Understanding DOM Elements

DOM stands for Document Object Model. It is a programming interface for HTML and XML documents, representing the structure of a document in a tree-like format. Each node in this tree-like structure is an object representing a part of the document—an element, an attribute, or text. The DOM allows languages like JavaScript to interact and manipulate pages dynamically.

Each node in the DOM tree has a relationship with other nodes. Nodes can have parent nodes, child nodes, and sibling nodes, forming a family tree of nodes. By understanding this hierarchy, you can traverse from one node to another and target the specific element you wish to manipulate.

Accessing DOM Elements with JavaScript

JavaScript provides a set of methods to access DOM elements. Here are the most commonly used ones:

  • getElementById(id): This method retrieves a reference to an element by its unique id.
  • getElementsByClassName(name): This method retrieves a NodeList containing all elements with the specified class name.
  • getElementsByTagName(name): This method retrieves a NodeList containing all elements with the specified tag name.
  • querySelector(selector): This method retrieves the first element that matches the specified CSS selector.
  • querySelectorAll(selector): This method retrieves all elements that match the specified CSS selectors.

Here's an example of accessing elements by id:

let element = document.getElementById("myElement");
Enter fullscreen mode Exit fullscreen mode

The document object represents your web page. If you want to access DOM elements with JavaScript, you will work with the document object.

Manipulating DOM Elements with JavaScript

Once you've accessed an element, you can manipulate it. Here are some basic manipulations:

  • Changing an element's text using innerText or textContent property.
  • Changing the HTML content of an element using innerHTML property.
  • Adding or removing an attribute using setAttribute() or removeAttribute() methods.

Example:

let element = document.getElementById("myElement");
element.innerText = "New text for my element";
element.setAttribute("class", "myClass");
Enter fullscreen mode Exit fullscreen mode

In this example, we've selected an element with the id of "myElement", changed its text, and added a class attribute.

Working with Styles and Classes

JavaScript can also manipulate the styles and classes of HTML elements. Here's how you can do it:

  • Using the style property to manipulate inline styles of an element. For example: element.style.color = "red";
  • Using classList to manipulate classes of an element. The classList object has methods like add(), remove(), and toggle() to work with classes.

Example:

let element = document.getElementById("myElement");
element.style.color = "red";
element.classList.add("highlight");
Enter fullscreen mode Exit fullscreen mode

In this example, we've changed the text color of the "myElement" to red and added a "highlight" class to it.

Advanced DOM Manipulation Techniques

DocumentFragment

A DocumentFragment is a minimal document object without a parent. It's used as a lightweight version of Document and improves performance by minimizing web page reflows (processes of recalculating the positions of elements in a document). It allows us to compose portions of a document in memory and then append them to the document as needed.

let fragment = document.createDocumentFragment();

let li;
for (let i = 0; i < 10; i++) {
    li = document.createElement("li");
    li.innerText = `Item ${i}`;
    fragment.appendChild(li);
}

document.getElementById("myList").appendChild(fragment);
Enter fullscreen mode Exit fullscreen mode

In this example, we're adding 10 list items to an unordered list using a DocumentFragment.

Traversing the DOM tree

There are several properties that allow traversing the DOM tree:

  • parentNode: Selects the parent of the current element.
  • children: Returns a collection of an element's child element.
  • firstElementChild and lastElementChild: Select the first and last child of the current element.
  • nextElementSibling and previousElementSibling: Select the next or previous sibling of the current element.
let parent = document.getElementById("myElement").parentNode;
let children = document.getElementById("myElement").children;
Enter fullscreen mode Exit fullscreen mode

In this example, we've accessed the parent and children of the "myElement".

Common Mistakes and Best Practices

In your journey of DOM manipulation with JavaScript, it's essential to be mindful of some common mistakes that developers often make and to follow the best practices to write clean, efficient, and error-free code.

Common Mistakes

1. Not checking for DOM readiness: JavaScript runs as soon as it is encountered. If your JavaScript code is trying to manipulate DOM elements that haven't been loaded yet, it can lead to errors. Always ensure your DOM is fully loaded before running your JavaScript code. This can be achieved using the DOMContentLoaded event or placing your script tag right before the closing </body> tag.

2. Overusing innerHTML: The innerHTML property is handy for getting or replacing HTML content. However, overusing it can lead to performance issues and potential cross-site scripting (XSS) attacks. It's more efficient to use textContent or innerText for changing the text of an element, and createElement, appendChild, and removeChild for changing the structure.

3. Manipulating DOM inside loops: If you manipulate the DOM inside a loop, the changes in each iteration can cause the browser to recalculate layouts and repaint the screen, which can be performance-intensive. Instead, try to make all your changes off the DOM (for instance, by creating a DocumentFragment), and then append the result to the DOM once after the loop.

Best Practices

1. Minimize direct DOM manipulation: Every time you manipulate the DOM, the browser may need to recalculate layouts and repaint the screen. These operations can be costly, so it's best to minimize your DOM manipulations. Instead of making several small changes, make one big change. A DocumentFragment is a great tool for this.

2. Use const and let over var: The var keyword in JavaScript has confusing scoping rules and can lead to unexpected bugs. In modern JavaScript, it's better to use const for variables that won't be reassigned, and let for variables that will.

3. Use event delegation for dynamic elements: If elements are being added and removed from the DOM dynamically, and you want to handle events on these elements, it can be inefficient to add and remove event listeners every time the elements change. Instead, add an event listener to a stable parent element, and use the target property of the event object to figure out which child element was interacted with.

With these tips and cautionary notes, you can avoid common pitfalls and write JavaScript code that is efficient, clean, and easy to maintain. Always remember to keep exploring new techniques and best practices, as the field of web development is always evolving.

Practical Examples

Now that we understand the theory behind manipulating the DOM with JavaScript, let's see it in action with some practical examples.

Building a Dynamic Navigation Menu

A navigation menu is a common feature on many websites. Here's how you can build one dynamically using JavaScript:

<!DOCTYPE html>
<html>
<body>

<ul id="nav"></ul>

<script>
let menuItems = ["Home", "About", "Services", "Contact"];
let nav = document.getElementById("nav");

menuItems.forEach(function(item) {
    let li = document.createElement("li");
    li.textContent = item;
    nav.appendChild(li);
});
</script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this example, we're creating an unordered list (<ul>) of menu items. We have an array of menu items and we iterate over this array using forEach. For each item, we create a list item (<li>), set its text, and append it to the <ul>.

Creating an Image Carousel

An image carousel is a slideshow of images that can be manually or automatically cycled through. Here's a simplified version of how you can create one:

<!DOCTYPE html>
<html>
<body>

<div id="carousel">
    <img src="image1.jpg" class="active">
    <img src="image2.jpg">
    <img src="image3.jpg">
</div>

<script>
let images = Array.from(document.getElementById("carousel").children);
let currentImageIndex = 0;

function rotateImages() {
    images[currentImageIndex].classList.remove("active");
    currentImageIndex = (currentImageIndex + 1) % images.length;
    images[currentImageIndex].classList.add("active");
}

setInterval(rotateImages, 3000);
</script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this example, we have a <div> containing several <img> elements. Every 3 seconds, we rotate the images by removing the "active" class from the current image and adding it to the next image.

Making a Form with Live Validation

Form validation is a critical part of any form handling. Let's see how you can implement live form validation using JavaScript:

<!DOCTYPE html>
<html>
<body>

<form id="myForm">
    <input type="text" id="username" placeholder="Username">
    <small></small>
    <input type="submit">
</form>

<script>
let form = document.getElementById("myForm");
let username = document.getElementById("username");

username.addEventListener("input", function() {
    if (username.value.length < 5) {
        username.nextElementSibling.textContent = "Username must be at least 5 characters long";
    } else {
        username.nextElementSibling.textContent = "";
    }
});

form.addEventListener("submit", function(event) {
    if (username.value.length < 5) {
        event.preventDefault();
        username.nextElementSibling.textContent = "Username must be at least 5 characters long";
    }
});
</script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this example, we have a form with a single input for the username. We use the input event to listen for any changes to the input. If the username is less than 5 characters, we display an error message. We also prevent form submission if the username is invalid.

These examples show the power and flexibility of JavaScript when it comes to manipulating the DOM to build dynamic, interactive features. With practice and further study, you'll be able to build even more complex features. Happy coding!

Conclusion

JavaScript is an essential language for web development, playing a critical role in adding interactivity and dynamism to websites. Its ability to manipulate the Document Object Model (DOM) is among its most powerful features, allowing developers to make websites more engaging and responsive to user actions.

In this guide, we've explored the fundamentals of the DOM and how you can interact with it using JavaScript. We've covered various topics from accessing and manipulating DOM elements, to working with styles and classes, and even delved into advanced techniques. Additionally, we've highlighted some common mistakes to avoid and best practices to follow. We further enhanced our understanding with practical examples such as building a dynamic navigation menu, creating an image carousel, and implementing a form with live validation.

As you continue on your journey with JavaScript, remember that mastering DOM manipulation takes practice. Experiment with different methods and tasks, build small projects and continue learning. Each line of code you write will take you one step closer to becoming proficient in manipulating the DOM with JavaScript.

By harnessing the power of the DOM, you'll be well-equipped to create rich, engaging web applications that offer an outstanding user experience. Keep exploring, keep coding, and above all, enjoy the journey!

Top comments (0)