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
  • 22,934
  • 9
  • 44
  • 60
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
  • 669,327
  • 51
  • 454
  • 560
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,354
  • 1
  • 1
  • 16
1

You could also assign the values manually:

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

table1.style.color = textColor;
table1.style.backgroundColor = bgColor;
code
  • 2,854
  • 4
  • 9
  • 28