JavaScript – Change the Background Color of HTML Element

To change the background color of a HTML Element using JavaScript, get reference to that HTML element, and assign required color value to the element.style.backgroundColor property.

In the following example, we will change the background color of HTML Element with id "myElement" to the color "#00FF00", in JavaScript, using element.style.backgroundColor property.

example.html

ADVERTISEMENT

Conclusion

In this JavaScript Tutorial, we learned how to change the background color of a HTML Element using JavaScript.