Changing Styles
element.style.property sets inline CSS using camelCase names (backgroundColor). A cleaner approach is to prepare classes in CSS and add or remove them with classList.add, remove and toggle.
Example
const style = document.createElement("style");
style.textContent = ".active { background: #166534; color: white; padding: 8px; }";
document.head.appendChild(style);
const box = document.getElementById("demo");
box.textContent = "Toggle me";
box.classList.add("active");
box.style.borderRadius = "8px";
console.log(box.classList.contains("active"));Exercise
Switch a class on or off.
box.classList.("active");
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.