4

I have hidden element in my webpage, I need to get height of that element. I have tried with .clientHeight, offsetHeight, .height(), and window.getComputedStyle but non of this working in attached scenario. Is there any trick to get height without adding any plugin. fiddle

HTML

<div class="frame">
<p>some text some text some text some text</p>
</div>

Jquery

$('p').height()
Jitender
  • 7,593
  • 30
  • 104
  • 210

5 Answers5

2

You could render it off-screen where the user can't see it, get its height, then restore the element back to normal.

It's better than using visibility: visible because it doesn't disrupt the positioning of the other elements on the page.


HTML

<div class="frame">
    <p>some text some text some text some text</p>
</div>

CSS

.frame {
    width: 120px;
    display: none;
}

.offscreen {
    position: fixed !important;
    left: -9999px !important;
    display: inline !important;
}

JavaScript

$('.frame').addClass('offscreen');

alert('The hieght of \'p\' is: ' + $('p').height() + 'px');

$('.frame').removeClass('offscreen');

Fiddle link: http://jsfiddle.net/HfyfX/

Joe Simmons
  • 1,828
  • 2
  • 12
  • 9
1
var p = $('.frame p').clone().css('display', 'none');
$('body').append(p);
alert(p.height()); // 19
p.remove();

Working example: http://jsfiddle.net/D9PyE/

snadon
  • 71
  • 1
  • 3
  • Cloning it is actually a great idea. That way, you don't have to disrupt the original element, no matter how it's set up. – Joe Simmons Oct 04 '13 at 04:07
0

Here's the FIDDLE

alert($('div').removeClass('frame').css('visibility','hidden').find('p').height());
$('div').addClass('frame').css('visibility','visible');

You can try this if you want. :)

Vond Ritz
  • 2,022
  • 14
  • 15
0

Try this out:- http://jsfiddle.net/adiioo7/teeFD/7/

CSS:-

.frame { width:120px; visibility: hidden;}
Aditya Singh
  • 9,512
  • 5
  • 32
  • 55
0

For get the height of hidden element you need to set CSS it :

$(".frame").css({ 'position': 'absolute', 'visibility': 'hidden', 'display': 'block' });

and after getting the height of element again set css:

$(".frame").css({'position':'static','visibility':'visible', 'display':'none'});

Try This

$(".frame").css({ 'position': 'absolute', 'visibility': 'hidden', 'display': 'block' });
alert($('p').height());
$(".frame").css({'position':'static','visibility':'visible', 'display':'none'});
Ishan Jain
  • 8,063
  • 9
  • 48
  • 75