Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to read out the "naturalWidth" of an image with jquery?

Tags:

I tried with something like this:

var Height = $(this).naturalHeight; 

But it doesn't work. Is there any way to do that

greez

like image 595
ValiL Avatar asked Dec 02 '09 13:12

ValiL


People also ask

How to get the width of image in jQuery?

var imageWidth = $(Imgsize). width(); alert(imageWidth);

What is img naturalWidth?

The naturalWidth property returns the original width of an image. For example, if you have an image that is originally 100 pixels wide. Then, you style the image with CSS/or by using the HTML "width" attribute to make it 500 pixels wide.

How to get width of an image in JavaScript?

Answer: Use the JavaScript clientWidth property You can simply use the JavaScript clientWidth property to get the current width and height of an image. This property will round the value to an integer.


1 Answers

I use the native javascript properties after applying jQuery and then stripping jQuery

.naturalWidth / .naturalHeight

On a jQuery object i use

var width = this.naturalWidth;  var height = this.naturalHeight; 

or

var width = $("selector").get(0).naturalWidth;  var height = $("selector").get(0).naturalHeight; 

if no jQuery object is selected.

With get(0) on a jQuery object you can access the associated DOM-element. On the native DOM element you can work with native javascript code (here access the nativ javascript attribute .naturalWidth)

like image 172
Ruwen Avatar answered Sep 21 '22 15:09

Ruwen