JQuery Height of hidden TR returned 0

I am trying to calculate the height TR which is set to display: none, however jQuery's height extension method returns 0 every time. Of course when its visible value returns the correct value. From my understanding and past experience, I don't know why this is happening, because what if it doesn't return the height even if it is hidden? Anyway, any help would be appreciated!

thank!

+2


source to share


2 answers


No, elements display:none

do not take up space in document space, so they have no parameters.

It was expected that properties that are computed from the element's rectangle (like offsetWidth

or offsetHeight

) will return 0

.



Ref: CSS1 Formatting Model

Edit : getting the sizes of hidden items is difficult. Depending on your situation, this answer might be helpful.

+4


source


Rotatin is right. What I've done in the past when I really needed to know what the height of an element is, is to use jquery to get the height, store it in the element's data object, and then hide the element after the page has loaded.

This will be unsightly for a page with a lot of elements (it seems to lag behind because the whole page will load before hiding the elements).

But, if you need this technique, it can be done as such:

$('.should_be_hidden').each(function() {
    $(this).data('height',$(this).height()).hide();
});

      



This is often useful if you need to animate an object.

To get the height when needed ... you can simply:

var element_height = $('#some_element').data('height');

      

+1


source







All Articles