Js dynamic styles?

I wants add a array of styles to an element with only js so i did like this.

var cssstyle = ['color'];
document.body.style.cssstyle[0] = '#fff';

i understood that why it is not working but i wants to do this so any one know how to do this?

If you want to access properties dynamically, you’ll have to use the square-brackets notation:

var cssstyle = ['color']
document.body.style[cssstyle[0]] = '#fff'

Or you could also apply multiple styles at once using Object.assign():

var styles = {
  color: '#fff',
  backgroundColor: '#000'

Object.assign(document.body.style, styles)

yeah just now i also got this. thanks for the help

something is a property so we have to send it as [something] as we use for the objects properties.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.