Jacob Folley
Jacob Folley

Reputation: 49

Template Literals with JavaScript DOM

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

Answers (1)

vityavv
vityavv

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

Related Questions