Finding Div Height with Javascript

Discussion in 'JavaScript' started by thrillseeker30, Apr 27, 2009.

  1. #1
    In my stylesheet, I have set the height of a div and in Javascript, I want to be able to reference that height to properly position the div in the center of the browser.

    var divHeight =document.getElementById("modal_window").offsetHeight;
    Code (markup):
    That is the piece of Javascript I have been using, but can't seem to get it to work. Using the alert() function, it returns an integer of 0, even though the height is set in CSS.

    Any ways to fix this problem and find the height of a div using Javascript?
     
    thrillseeker30, Apr 27, 2009 IP
  2. JavaScriptBank.com

    JavaScriptBank.com Peon

    Messages:
    141
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you must set the hieght in stylesheet within your HTML
     
    JavaScriptBank.com, Apr 27, 2009 IP
  3. thrillseeker30

    thrillseeker30 Guest

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That would negate the purpose of using an external stylesheet. Inline styling just makes HTML look messy.

    I figured it out though....since display:none was set on the DIV, it wasn't being rendered with the webpage. Since it wasn't being rendered, it had no height value. I just switched the order of how my Javascript was read and wa la!
     
    thrillseeker30, Apr 28, 2009 IP