ReviseAlgo Logo

DOM Manipulation

Modifying Attributes & Classes

Master modifying element properties in JavaScript. Compare getAttribute, setAttribute, dataset properties, and classList methods.

Last Updated: July 15, 2026 10 min read

1. Introduction

In order to build interactive user interfaces, you must be able to update element properties dynamically. JavaScript provides APIs to modify element attributes, class lists, inline styles, and custom data attributes.

2. Why It Matters

Managing styling changes by editing inline styles directly makes CSS difficult to maintain. Using the classList API is a cleaner approach because it lets you toggle CSS classes, keeping styling rules separated in your stylesheets.

3. Real-World Analogy

Think of a Smart Office Dashboard Display:

  • Attribute (Physical Plaque specs): Changing the properties of a wall plaque, like its height, name, or serial number.
  • classList (Status Indicators): Sliding indicator panels onto the plaque (e.g. adding a "Busy" or "Away" class). Instead of repainting the entire plaque, you simply swap status tags to change its appearance.
  • dataset (Sticky Notes on the Back): Placing a sticky note with helper information on the back of the plaque ("Assigned to Alice"). The information is hidden from visitors but is easily readable by technicians.

4. Class & Attribute APIs

Let's explore the common methods for modifying elements:

1. Modifying Attributes:

  • getAttribute(name): Returns the value of the specified attribute.
  • setAttribute(name, value): Sets the value of the specified attribute.
  • removeAttribute(name): Removes the specified attribute.

2. Modifying Classes (classList API):

  • classList.add(className): Adds the specified class to the element.
  • classList.remove(className): Removes the specified class from the element.
  • classList.toggle(className): Toggles the class (adds it if missing, removes it if present).
  • classList.contains(className): Returns true if the class is present on the element.

3. Custom Data Attributes (dataset):

HTML elements support custom attributes prefixed with data-. These attributes are exposed in JavaScript as a camelCase object via the dataset property.

5. Practical Example

This script demonstrates implementing a dark mode toggle button that updates both classes and custom data attributes:

6. Common Mistakes

  • Modifying className directly: Overwriting the className property directly (e.g. element.className = 'active') replaces all existing classes. Use classList.add() or classList.remove() instead to keep other classes intact.
  • Assuming dataset keys are lowercase: Custom data attributes are declared with hyphens in HTML (e.g. data-user-id) but are converted to camelCase in JavaScript (e.g. dataset.userId). Accessing them with lowercase keys (e.g. dataset.userid) returns undefined.

7. Quick Quiz

Q1: Which method should you use to check if an element contains a specific class without mutating the class list?

A) classList.contains()

B) classList.toggle()

Answer: A — classList.contains() returns a boolean indicating whether a class is present, without modifying the class list.

8. Scenario-Based Challenge

The Multi-Step Accordion Toggle:

You are designing an accordion menu: .accordion-item. Clicking an item toggles its class to open or closed, and updates the aria-expanded accessibility attribute. Write the click callback that handles this state toggle.

9. Debugging Exercise

Explain why this dataset lookup returns undefined, and how to fix it:

<!-- HTML -->
<div id="product" data-product-price="25"></div>
// JavaScript
const prod = document.getElementById('product');
console.log(prod.dataset['product-price']); // logs undefined! Why?
View Solution

Diagnosis: The browser parses hyphenated data attributes into camelCase object properties inside the dataset object. The key data-product-price is mapped to productPrice.

Fix: Access the property using its camelCase key name:

console.log(prod.dataset.productPrice); // 25

10. Interview Questions

🟢 Q1: Compare className and classList for managing HTML classes.

Answer:
className is a string property representing the entire class attribute. Modifying it replaces all existing classes, which can overwrite other classes unexpectedly.
classList returns a token list object containing helper methods (like add, remove, toggle, and contains) that allow you to modify individual classes safely without affecting other classes.

11. Production Considerations

  • Separate Styling Concerns: Avoid modifying inline style attributes directly (e.g. element.style.color = 'red'). Instead, define style rules in your CSS stylesheets and toggle classes on the elements using classList to keep styles separated from layout logic.