tags:

views:

62

answers:

6

Hi,

i want to to get the height/width of an image inside a hidden div-containter, but .height() and .width() both returns 0 (like expected).


$('body').append('<div id="init"><img id="myimg" src="someimage.png" /></div>');
$('#init').hide();
$('#myimg').height(); // == 0
$('#myimg').width(); // == 0

How can i get the correct height/width of the image? I need it to make some decisions :-)

Best regards, Biggie

+1  A: 

Make the div temporarily visible in order to compute the height or hide the div off screen instead of using display: None. I have used the first approach, and found it to be fast enough that you will never see the element.

mikerobi
Yeah, and even if it's slow enough to show it something like an absolute position plus a -1000 margin should hide it during the period that it's visible.
Litso
+3  A: 

You can't hide it if you want the correct height and width, but you can move it such that it will be out of the screen, and thus effectively hidden. Try this:

var hiddenDiv = $('<div id="init"><img id="myimg" src="someimage.png" /></div>').appendTo('body').css({
    'position': 'absolute',
    'top': -9999
});

var img = hiddenDiv.children('img');
img.height();
img.width();
Yi Jiang
As you pointed out on my post, This is loading a cached image, The OP should review both answers.
RobertPitt
@Robert I've done the testing - `display: none` elements do have height - typing in `$('#answer-3761104').hide().height()` gives the correct height - so this answer is entirely wrong.
Yi Jiang
I thought it would of been down to the `image load event`.
RobertPitt
@Yi Jiang, RobertPitt: I had no problems with this solution, because i used a bind to 'load' by intuition wherein i call height() and width() :-) So this works as good as the solution of RobertPitt.
Biggie
A: 

it looks like you need to compute the height & width of an image. if so, you're thinking of it too hard.

Martin Ongtangco
-1 Unless you explain *why*, or what his alternatives might be, this is not useful.
egrunin
*You're* thinking of it too hard.
Stephen
A: 

you have to use find()

$('body').find('#myimg').width();

or

$('body').find('#myimg').attr("width");

becouse you have appended it after the DOM was loaded

Fincha
Wrong, why would we need to find when we have an ID ?
RobertPitt
i had the same problem, and i have fixed it, by using find().
Fincha
A: 

I'm not sure you can do it with jQuery (or javascript at all). I had a similar problem with misreported heights and widths on loading/hidden img's. You can, however, wait till the image loads, get it's correct height and width and then hide the parent... like so:

var height, width;

$('#myimg').load(function() {
    height = $(this).height();
    width = $(this).width();
    $('#init').hide();
}
bschaeffer
+1  A: 

Its because the image has not loaded yet.....

$('body').append('<div id="init"><img id="myimg" src="something.png" /></div>');
$('#init').hide();

$('#myimg').bind("load",function(){
    alert(this.height) //works
})​

heres a test: http://jsfiddle.net/WMpSy/

RobertPitt