Reputation: 49
I am wondering as to why this template literal does not work when I apply it using JavaScript DOM for css style? Your help in understanding this is greatly appreciated!
let rainColors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
if (rainbows == true) {
row.style.backgroundColor = '${rainColors[getRandomInt(6)]}';
Upvotes: 0
Views: 89
Reputation: 1490
It doesn't work because you're using single quotes. you need to use backquotes, like this:
let rainColors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'];
if (rainbows == true) {
row.style.backgroundColor = `${rainColors[getRandomInt(6)]}`;
By the way, there is no reason to do this:
`${variable}`
As you can just use
variable
Upvotes: 3