SOLVED: Syntax-Error
The following code works:
var image = {width:0,height:0};
var imageObject = new Image();
function getImageSize(id) {
imageObject.src = id.attr("src");
image.width = imageObject.width;
image.height = imageObject.height;
}
Original Question
I want to get the original width and height of an image and tried different code to achieve it. My function triggers on click event and all images are resized to 400x300.
I tried to resize the Image to its original size by using auto
:
var image = {width:0,heigth:0};
function getImageSize(id) {
id.css({
'width': 'auto',
'heigth': 'auto'
});
image.width = id.attr('width');
image.heigth = id.attr('heigth');
}
Attaching auto
didn't change the size of the image.
alert(id.attr('width'));
always returns 'undefined'. so it doesn't work
var image = {width:0,heigth:0};
var imageObject = new Image();
function getImageSize(id) {
imageObject.src = id.attr("src");
image.width = imageObject.width;
image.heigth = imageObject.heigth;
}
alert(image.width);
works perfectly.
alert(image.heigth);
returns undefined.
Using imageObject.naturalWidth
works as the one above - the width is given correctly but the heigth is undefined.
I read that you have to append the new image object, but when I use imageObject.appendTo('body');
I can't even click the image.
Thanks for any suggestions.