Take the 2-minute tour ×

  function getDocumentHeight()
     // What's the page height?
     var height = $('#mainbody').height();

jQuery keeps alerting "null" for the height of my #mainbody div. i don't get why, it should at least be 500px or so.

thank you.

share|improve this question
You sure <div id="mainbody"> exists? –  Marko Sep 9 '10 at 21:47
You don't have id="#mainbody", with the # in it do you? –  Nick Craver Sep 9 '10 at 21:49
@Nick: +1 for psychic debugging. –  Tomalak Sep 9 '10 at 21:51
@Tomalak +1 for the phrase "psychic debugging" –  Marko Sep 9 '10 at 21:55
@Marko - +1 for phrase praise –  Nick Craver Sep 9 '10 at 21:57
show 1 more comment

1 Answer

up vote 7 down vote accepted

If the content is being loaded dynmically, the elements may not have a height yet. Try moving this code to the bottom of the page, directly above and see if that resolves.

UPDATE: Try placing your code in

$(window).load(function() {

outside of your

share|improve this answer
I have this problem, but my code executes after the DOM is ready. Wierd. –  trusktr Dec 20 '11 at 10:30
add comment

Your Answer


