0Pricing
JavaScript Academy · Lesson

Modifying Attributes and Classes

Use setAttribute and the classList API.

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"));

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