1

I have 2 variables and i need to make this as a color. For example:

table1.style.cssText = "color: textcolor; background-color: bgcolor; "

where bgcolor and textcolor - variables with color value (red/black for example)

VLAZ
  • 26,331
  • 9
  • 49
  • 67
David
  • 23
  • 4
  • [How to change CSS property using JavaScript](https://stackoverflow.com/q/15241915) – VLAZ May 12 '22 at 17:55

3 Answers3

2

Use a template literal.

table1.style.cssText = `color: ${textcolor}; background-color: ${bgcolor}; "
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You can inject variables into a string using a template literal string:

const textColor = 'white';
const bgColor = 'black';

table1.style.cssText = `color: ${textColor}; background-color: ${bgColor};`;

Demo:

const msg = 'hello';
const msg2 = 'world';

console.log(`${msg} ${msg2}!!!`);
mstephen19
  • 1,733
  • 1
  • 5
  • 20
1

You could also assign the values manually:

const textColor = 'white';
const bgColor = 'black';

table1.style.color = textColor;
table1.style.backgroundColor = bgColor;
code
  • 5,690
  • 4
  • 17
  • 39