0

I'm trying to get the height of an initially hidden div element in Netscape and Firefox using javascript (it works fine in IE). I have tried using MyElement.scrollHeight, MyElement.offetHeight, MyElement.style.height and many different ways of setting the element initially visible, getting the height and then hiding it again. I keep getting a value of 0. Any thoughts?

Kris
  • 1
  • 1
  • 1

2 Answers2

1

Correct me if I'm wrong (Which I may be), but from doing some research, elements with display: none have no height.

To get the height, you would need to unhide them, get the height, then rehide them. Issues like this have popped up in the past like so:

jQuery: height()/width() and "display:none"

jQuery: Get height of hidden element in jQuery

(Both jQuery examples, but you get the point)

Community
  • 1
  • 1
David
  • 2,053
  • 2
  • 16
  • 26
0

Hidden elements in Netscape is not accessible.

you can set that position with styling hole of div out of your screen. for example move it to -9999 on top left. then you can access that's attributes like height and width.

you can also visible that element and get your attribute and hide it again, but it is not usual because your element will be show a little bit of the second and user maybe feel jumping on screen.
you should position it by setting style to "absolute" and "top" to -9999px or more (depend of your project height) and then get your element height and width with DOM and anythings that you want ...

Reza Amya
  • 89
  • 1
  • 10
  • Thanks for your help guys. It turns out that, because I had nested p elements within the hidden div, I could access their scrollHeights, since they weren't hidden elements. The many solutions I have read about showing a div, getting the element height, and then hiding it again do not seem to work - perhaps because of the nested p elements? – Kris Sep 24 '12 at 10:46