0

In the following code, I am trying to set backgroundColor via a variable prop. I would like to know the reason as to why div.style.prop does not work.

var div = document.getElementById('test')

var prop = 'backgroundColor'
div.innerHTML = 'This is some text'

div.style.prop = 'red'
b Tech
  • 412
  • 4
  • 14
  • 1
    `div.style[prop] = 'red'` – Agney May 09 '18 at 14:15
  • your `var prop` has nothing to do with `div.style.prop`, they are two different variables. `div.style.prop` doesn't exist either, unless you create it (which is pointless). Do as the above comment suggests. – ADyson May 09 '18 at 14:15

0 Answers0