Setting the width and heights value in div's style tag from variables

16,326

The following assumes that my_height and my_width returned by your functions are plain numbers:

document.getElementById('mygraph').style.height = my_height + "px";
document.getElementById('mygraph').style.width = my_width + "px";
Share:
16,326
goseib
Author by

goseib

Updated on June 05, 2022

Comments

  • goseib
    goseib almost 2 years

    In my javascript file I have the following line:

    <div id="mygraph" style="width:200px;height:100px;"></div>
    

    I'd like to set the width and height values in the style tag from two variables that take their values from the corresponding functions:

    var my_width = getWidth();
    var my_height = getHeight();
    

    How can I accomplish that? I'm missing the correct syntax. Thank you in advance...

  • goseib
    goseib almost 12 years
    Thanks Zoltan. I included these two lines in the <script></script> and I removed the style tag from the div like this: <body><div id="mygraph"></div></body>. But, the graph does not appear! I feel I'm doing something fundamentally wrong :( Any suggestions?
  • goseib
    goseib almost 12 years
    Thank you Zoltan! This is what I wanted to do. Problem solved!