How to set background color of HTML element using css properties in JavaScript

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)
{ = color;

// where el is the concerned element
var el = document.getElementById('elementId');
setColor(el, 'green');