Tsallaka zuwa abun ciki
Justice Tech School

Attributes, Classes and Dataset

getAttribute and setAttribute read and write attributes. classList adds, removes and toggles classes. dataset reads data-* attributes.

Example
const b = document.createElement("button");
b.setAttribute("data-id", "7");
b.classList.add("btn", "primary");
b.classList.toggle("primary");
b.textContent = "Button " + b.dataset.id;
document.body.appendChild(b);
console.log(b.className);

Exercise

Toggle a class.

el.classList.("on")

Quiz Test

How do you read data-id?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.