0Pricing
JavaScript Academy · Lesson

Modifying Attributes and Classes

Use setAttribute and the classList API.

Modifying Attributes and Classes is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Attributes and Classes

Elements carry attributes (like href or data-id) and CSS classes. JavaScript gives you precise APIs to read and change both.

getAttribute

getAttribute reads an attribute’s value as a string, or returns null if it is absent.

const link = document.querySelector("a");
console.log(link.getAttribute("href"));

setAttribute

setAttribute creates or updates an attribute. Values are always strings.

link.setAttribute("href", "https://example.com");
link.setAttribute("target", "_blank");

removeAttribute and hasAttribute

Use removeAttribute to delete one and hasAttribute to check existence.

if (link.hasAttribute("target")) {
  link.removeAttribute("target");
}

Properties vs Attributes

Many attributes also exist as element properties (like element.id or input.value). Properties reflect current state, while attributes often hold the initial HTML value.

input.value;                 // current value
input.getAttribute("value"); // original default

dataset for data-*

Custom data-* attributes are exposed via the dataset object using camelCase keys.

const card = document.querySelector(".card");
console.log(card.dataset.userId); // from data-user-id
card.dataset.state = "open";       // sets data-state

classList Overview

classList is a live object for managing classes without manual string editing of className.

const box = document.querySelector(".box");
console.log(box.classList);

classList.add and remove

add and remove accept one or more class names and ignore duplicates or missing classes gracefully.

box.classList.add("active", "highlight");
box.classList.remove("hidden");

classList.toggle

toggle adds the class if absent and removes it if present, returning a boolean of the new state. A second argument forces on or off.

box.classList.toggle("open");
box.classList.toggle("open", true); // force add

classList.contains and replace

contains checks membership and replace swaps one class for another in place.

if (box.classList.contains("active")) {
  box.classList.replace("active", "inactive");
}

Why classList Beats className

Editing className as a string risks clobbering other classes. classList changes only the targeted classes safely.

box.className = "active"; // wipes all other classes
box.classList.add("active"); // keeps the rest

Quick Check

Pick the toggle behavior.

Recap

Read and write attributes with getAttribute/setAttribute/removeAttribute/hasAttribute, and access data-* via dataset. Manage classes with classList methods add, remove, toggle, contains, and replace, which are safer than overwriting className.

Frequently asked questions

Is the “Modifying Attributes and Classes” lesson free?

Yes — the full text of “Modifying Attributes and Classes” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Modifying Attributes and Classes”?

Use setAttribute and the classList API. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Modifying Attributes and Classes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Changing Text and HTML Content
  2. Creating and Inserting Elements
  3. Modifying Attributes and Classes
  4. Removing and Replacing Nodes
← Back to JavaScript Academy