mkoryak mkoryak - 2 years ago 282
Javascript Question

jQuery: Get height of hidden element in jQuery

I need to get height of an element that is within a div that is hidden. Right now I show the div, get the height, and hide the parent div. This seems a bit silly. Is there a better way?

I'm using jQuery 1.4.2:

optionHeight = $firstOption.height(); //we can only get height if its visible

Answer Source

You could do something like this, a bit hacky though, forget position if it's already absolute:

var previousCss  = $("#myDiv").attr("style");

        position:   'absolute', // Optional if #myDiv is already absolute
        visibility: 'hidden',
        display:    'block'

optionHeight = $("#myDiv").height();

$("#myDiv").attr("style", previousCss ? previousCss : "");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download