nhaa123
nhaa123

Reputation: 9798

Get jQuery created element's height

When I create elements like this:

var a = $('<div></div>').text('foo');
var b = $('<div></div>').css('margin-bottom', '16px').text('bar');

console.log(a.height());      // Returns 0.
console.log(a.innerHeight()); // Returns 0.
console.log(a.outerHeight()); // Returns 0.

console.log(b.height());      // Returns 0.
console.log(b.innerHeight()); // Returns 0.
console.log(b.outerHeight()); // Returns 0.

then, how do I get their current height before I attach them to DOM?

Upvotes: 0

Views: 65

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337550

You can't.

You cannot get the height of any element unless it is part of the DOM. As a workaround you could append the element and set it to appear far off the screen, for example left: -9999px, and then grab its dimensions there before moving it to its desired location or removing it.

Upvotes: 2

Related Questions