How to set css style with Javascript dynamically using a variable for the dom style property. In other words, instead of having to explicitly elem.style.background = '#eee'
, elem.style(javascript variable) = '';
The most portable and reusable way to be able to set dom element style properties using any property you wish, using the simple Javascript function below.
Example of how to use, if you want to change the fontsize of a paragraph element:
setStyle(document.querySelectorAll('p'), 'fontSize', '24px')
Example of how to use, if you want to change multiple properties of an element at the same time:
setStyle(document.querySelectorAll('#divexample'), ['backcolor', 'color', 'position'], ['#fff', 'green', 'absolute'])