How can I get the height of an element which has got a parent element that has display: none
?
An example here: jsfiddle.net
Thanks
Lukas
How can I get the height of an element which has got a parent element that has display: none
?
An example here: jsfiddle.net
Thanks
Lukas
Temporarily show()
ing an element to retrieve a child's height seems to work OK.
HTML:
<div id="target" style="display:none;">
<!-- Add a background color to see if it's noticeable -->
<div style="height:123px; background:#000;"></div>
</div>
JS:
// Show the hidden parent of the element we want the height of
$("#target").show(0, function(){
// Find and grab the height of the element we want
the_height = $(this).find("div").height();
// Hide parent and display child's height after it
}).hide().after(the_height);
Demo: jsfiddle.net/Gts6A/72
You can do this or you can use the hack from this question.
$("#target").parent().show();
var h = $("#target").height();
$("#target").parent().hide();
alert(h);
See fiddle.
it's very difficult(in other word you can't)
to get the height of hidden element...because dom doesn't consider hidden elements while rendering the page
.
Hidden elements have an undefined width and height, so it's not possible to get em.
This is a bit klunky but you have to have an object rendered before it can be measured.
var $target = $("#target");
$target[0].parentNode.runtimeStyle.display = "block";
var height = $target.height();
$target[0].parentNode.runtimeStyle.display = "none";
alert(height);
Either you should display the div or it is impossible to get the height of the hidden elements. I would say, use .show() to show the div, get the height and then use .hide() to hide it again..