How can I set the background color of an HTML element using css in JavaScript?
In general, CSS properties are converted to JavaScript by making them camelCase without any dashes. So background-color
becomes backgroundColor
.
function setColor(element, color)
{
element.style.backgroundColor = color;
}
// where el is the concerned element
var el = document.getElementById('elementId');
setColor(el, 'green');