javascriptcssdatagridtemplatecolumn

Insert variable as parameter


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 ?


Solution

  • ${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)`;