JS DOM: Change CSS

By Xah Lee. Date: . Last updated: .

Press the following buttons to change the color.

Color Me

Code

<p>
 <span id="Jb3MX"><b>Color Me</b></span>
 <button id="red3331" type="button">RED</button>
 <button id="blue4640" type="button">BLUE</button>
</p>
{
 const Jb3MX = document.getElementById("Jb3MX");
 const red3331 = document.getElementById("red3331");
 const blue4640 = document.getElementById("blue4640");

 red3331.addEventListener("click", () => {
  Jb3MX.style.color = "red";
 });

 blue4640.addEventListener("click", () => {
  Jb3MX.style.color = "blue";
 });
}

This technique can be used to change the CSS value for any HTML element. You can also get the element by class or tag name.

JavaScript DOM, Change CSS

JS DOM Common Task Examples