Ahmed Ahmed - 5 months ago 21
jQuery Question

Get div height in pixels although its height set to 100%

I wonder if there is any way to get div height in pixels, although its height set earlier to 100% height.

This is required as div content is dynamic so div height has different values based on content itself.

[Edit]
Div by default is hidden.

I need to get div height in pixels for later manipulation (smooth scrolling will be done for div)?

Is there any way to do this?

Answer

Since you tagged jQuery, use

$("#myElement").height();

http://api.jquery.com/height/

For Plain Ol' Javascript, you can use element.clientHeight or element.offsetHeight, depending on which one suits your needs.


Since the div is hidden, you will have to .show() it before calling .height(), and you can hide it again straight away:

var $myEl  = $('#myElement').show();
var height = $myEl.height();
$myEl.hide();
Comments