Link to home
Start Free TrialLog in
Avatar of Daniele Brunengo
Daniele BrunengoFlag for Italy

asked on

Natural height and jquery

Hello, I am quite the jquery newbie and I'm trying to get the height of an image file. I mean the actual height of the image, not as it appears on screen.

So I wanted to use naturalHeight and I did this:

var nath = jQuery('.vc_single_image-img', this).get(0).naturalHeight;

Open in new window


It's inside a function applied to the row containing the image. There is a single image with the vc_single_image-img class.

I used get(0) to "convert" the jQuery object to the standard DOM element. I thought it would work, but I keep getting this error:

TypeError: jQuery(...).get(...)

Open in new window


I'm clearly missing something big here, as I said I'm a newb. So can somebody help me? Thanks.
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

HTML:
<img src="http://www.battlefieldseries.de/wp-content/uploads/2016/09/battlefield-1-beta-wallpaper-3-662x335.jpg" alt="" id="myImage" />

Open in new window

Using the height() function you get the height of the image element
JQuery
var heightImage=$('#myImage').height();
console.log(heightImage);

Open in new window

jQuery returns an array so to get the actual element

Try this
var nath = jQuery('.vc_single_image-img', this)[0].naturalHeight;

Open in new window

Avatar of Daniele Brunengo

ASKER

@Leonidas, I don't need the height of the element but of the actual file.
Anyway that's not the problem, because I get the same error if I use height. The problem is in the jQuery(...).get(...) syntax, the script doesn't even reach the following command (height or naturalHeight).

@Julian, sorry for not mentioning this, I had already tried that solution. I get the same error.

I also tried with children() and find() but the error is the same.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, this works.
You are welcome.