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)

1

3 Answers 3

2

Use a template literal.

table1.style.cssText = `color: ${textcolor}; background-color: ${bgcolor}; " 
Sign up to request clarification or add additional context in comments.

Comments

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}!!!`);

Comments

1

You could also assign the values manually:

const textColor = 'white'; const bgColor = 'black'; table1.style.color = textColor; table1.style.backgroundColor = bgColor; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.