Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why jquery .height() get a different result on chrome?

Tags:

jquery

This is how chrome show the width and height of the div :

enter image description here

which is correct, in fact the height is 1466. But, if I do this :

$(document).ready(function () {
    console.log($('#container-altezza-fisso').height());
});

it prints 1418. It doesnt have any padding/margin. Why? And how can I fix it?

like image 885
markzzz Avatar asked Oct 03 '12 13:10

markzzz


2 Answers

That's because on DOMReady some images are not loaded completely. You should call the height on window load.

$(window).load(function(){
    console.log($('#container-altezza-fisso').height());
})

You can also use outerHeight:

Get the current computed height for the first element in the set of matched elements, including padding, border, and optionally margin. Returns an integer (without "px") representation of the value or null if called on an empty set of elements.

console.log($('#container-altezza-fisso').outerHeight());
like image 170
undefined Avatar answered Oct 16 '22 11:10

undefined


Use outerHeight() to get the height with paddings. Use outerHeight(true) to get the height with paddings + margins.

Here's a link to the documentation.

like image 34
Konstantin Dinev Avatar answered Oct 16 '22 10:10

Konstantin Dinev