thelolcat
thelolcat

Reputation: 11545

How do I get the "auto" height of a DIV

So when I set a fixed height on a div with jquery, like $('div').height(200);, the value of $('div').height() is always 200. Even if the content from that div exceeds that height (I use overflow:hidden).

How can I get that true height of the DIV as if it would be in "auto" mode ?

Upvotes: 2

Views: 319

Answers (2)

Mina Gabriel
Mina Gabriel

Reputation: 25080

USE

.scrollHeight()

element.scrollHeight

with jquery try this

 $(selector)[0].scrollHeight

DESCRIPTION

An element's scrollHeight is a measurement of the height of an element's content including content not visible on the screen due to overflow.

Example

enter image description here

DEMO from Vega's Answer

Upvotes: 6

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

You mean hidden content height?

Vega - yes. The height of the content of the div, including the part that gets hidden by overflow

Just set to auto and get the .height and set it back to the fixed height.

var $el = $('#test');
var tmp = $el.css('height');

var actualHeight = $el.css('height', 'auto').height();

$el.css('height', tmp);
alert(actualHeight);

DEMO: http://jsfiddle.net/sKZfF/

Upvotes: 3

Related Questions