Insert variable as parameter

58 Views Asked by At

I have Javascript code :

document.getElementById("Grid").style.gridTemplateRows = "repeat(3, 2fr)";

How do I insert variable as argument to modify my CSS style, when I try:

"repeat(${MyVariable}, 2fr)"; 

it seems doesn't work ?

1

There are 1 best solutions below

0
mplungjan On BEST ANSWER

${MyVariable} belongs in backticks (template literals)

so EITHER

document.getElementById("Grid").style.gridTemplateRows = "repeat("+MyVariable+", 2fr)";

OR

document.getElementById("Grid").style.gridTemplateRows = `repeat(${MyVariable}, 2fr)`;