Lesson 55: Updating styles, attributes and classes
01Opening question
A tab looks selected, a button looks disabled, a row is highlighted. How does JavaScript change how elements look and behave without rewriting the CSS?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
Classes carry the styling; attributes describe state for the browser and assistive technology.
Learning objective: Add, remove and toggle classes, read and set attributes and data-* values, set inline styles sparingly, and keep ARIA attributes in sync with the visual state.
Skills practised: The DOM, Attributes, classes and styles.
04Explanation
el.classList.add("active"), .remove(...), .toggle(...) and .contains(...) manage CSS classes. Let CSS decide what a class looks like.
el.getAttribute("href") and el.setAttribute("href", "/shop") read and write HTML attributes; removeAttribute deletes one.
data-* attributes store small values on elements: data-id="7" is read with el.dataset.id (always a string).
el.style.color = "red" sets an inline style. Use it for values computed in JavaScript (positions, sizes); prefer classes for everything else.
Boolean attributes such as disabled, hidden and checked are on when present, whatever their value: setAttribute("disabled", "false") still disables. Use the property (btn.disabled = false) or removeAttribute.
Keep ARIA state in sync with visuals: aria-selected, aria-expanded and aria-pressed tell screen readers what the classes show.
getComputedStyle(el) returns the final styles after CSS is applied; el.style shows only inline styles.
A design system defines .is-selected, .is-loading and .has-error in CSS. Scripts only add and remove these classes (plus the matching ARIA attributes), so designers can restyle states without touching JavaScript.
el.classList.add("has-error") with the styles in CSS
el.setAttribute("style", "...") with every property
Replace the element with a new red one
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the class name: Prints true; Class is exactly highlight
Flag expensive items: Right items flagged; Uses dataset
Tab switcher: Prints the visible panel; ARIA in sync; Switch back to the first tab
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-11-the-document-object-model/lesson-055-updating-styles-attributes-and-classes/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
classList manages classes; CSS decides the look.
getAttribute/setAttribute work with any attribute; dataset reads data-*.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationclasses, attributes and styles
Context: I am learning classes, attributes and styles in JavaScript.
Task: Explain classList, attributes, dataset and inline styles, and when to use each.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionclasses, attributes and styles drill
Context: I have practised classes, attributes and styles.
Task: Give me six snippets that change classes and attributes and ask what each prints.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingclasses, attributes and styles bug
Context: My button stays disabled even though I set disabled to false.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationclasses, attributes and styles practice
Context: I finished the classes, attributes and styles exercises.
Task: Create three exercises that switch visual state with classes and ARIA.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewclasses, attributes and styles review
Context: Here is my code that uses classes, attributes and styles.
Task: Review my DOM code for inline styles that should be classes and missing ARIA updates.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.