views:

81

answers:

5

I need to be able to store the current height and width of a div at any point in time.

Currently I am using div.style.height/width which works fine when the styling is applied inline.

The problem is that this returns for example 600px and not 600.

Is there a better way to do this? If not, whats the best way to get just the number 600?

My updated code looks like this:

var div = document.getElementById('container');
div.scrollLeft = contentdim.cx*scalar - parseInt(div.style.width)/2;
div.scrollTop = contentdim.cy*scalar - parseInt(div.style.height)/2;

Which works fine in FF. For some reason scrollTop is messing up in Chrome though..

Note: This is a function which is called onscroll for the div.

+3  A: 
div.style.height.replace("px","")

With jQuery, a bit more elegant, you can do it as well: http://api.jquery.com/height/

$("div").height() 
//returns just the integer
Dustin Laine
so this is the best way to retrieve the height/width?
Shaunwithanau
+4  A: 

try div.offsetHeight || div.clientHeight

Mahesh Velaga
By far one of the most USEFUL properties that is not well known.
Zoidberg
When I tried to check offsetHeight it was null. Do I define the height of the div as its offsetHeight also?
Shaunwithanau
did u check if you have got the div element properly ?
Mahesh Velaga
Yes. I can use div.style.height just fine. I can also check div.scrollLeft/Top if that matters.
Shaunwithanau
Could you please post your javascript code ?
Mahesh Velaga
div.offsetHeight || div.clientHeight are likely what you're looking for. offsetHeight and clientHeight are not part of the style object.
ajm
Thank you ajm. That is a very important distinction.
Shaunwithanau
@ajm : thanks for pointing out, edited :)
Mahesh Velaga
A: 

Use jQuery:

height()

width()

Chris Doggett
I can't use jQuery.
Shaunwithanau
+2  A: 

parseInt(div.style.height) is more generic than div.style.height.replace("px","")

However div.style.offsetHeight might be better because it does not rely on style being explicitly set (but you have to render the div before you can read the value)

plodder
I think that parseInt(div.style.height) is my best option. However I still haven't been able to get offsetHeight to return anything but null
Shaunwithanau
The element has to be visible (i.e. not 'display:none') and already rendered
plodder
The div will always be visible but I believe your second point is what is messing it up. See my edited note
Shaunwithanau
This is the best answer for me, as it fixed the problem. The only issue I am still having is that for some reason scrollTop is always zero in Chrome. Does anyone know why this is?
Shaunwithanau
+2  A: 

To summarize the above:

document.getElementById('yourElement').style.height

CSS height if it has been set in a stylesheet. Will not include padding, margin, etc. If it is not set, you may wind up with values like auto.

document.getElementById('yourElement').offsetHeight

Height of the HTMLElement as rendered in the browser, including padding, scrollbars etc. (The total offset this Element's height consumes). Note that this often can be equivalent to clientHeight but that is not guaranteed.

Here is how offsetHeight is defined on Mozilla Developer Center. Note that there are a few differences with clientHeight, namely clientHeight does not include rendered scrollbars; offsetHeight will generally give you the maximum value.

ajm
The height is indeed what I wanted. Thank you for this answer though as it was informative and helpful.
Shaunwithanau