freecoding.school100% FREE · NO SIGNUP
The Old NetISSUE #53 of 60

toggle patterns · classList.toggle

Pixel PeteVSQuirks Mode
Pixel Pete saysToggle patterns flip a class or state to switch between two appearances.

classList.toggle("active") is the cleanest toggle: add the class if absent, remove if present, and it returns the new state. Use it for accordions, theme switches, "read more", and active nav items — define both states in CSS and let one line of JS flip between them. Keep the visual logic in CSS, the trigger in JS.

Power-ups you unlock

Quirks Mode attacks — common mistakes

Boss battleToggle a dark/light theme class on the body and style both in CSS.

Example code

<!doctype html><html><head><meta charset="utf-8"><style>body{background:#06040d;color:#e6e0ff}body.light{background:#f5f0ff;color:#1a1133}</style></head>
<body style="padding:20px"><button id="b">toggle theme</button>
<script>document.getElementById('b').onclick=()=>document.body.classList.toggle('light');</script></body></html>
▶ Open the interactive comic issue
‹ Show / Hide Patterns · Display · VisibilityAnimations The Dhtml Way · SetInterval + Style ›