Larry
Larry

Reputation: 1239

How to get width of an image after setting the height

I have set the height of my images to 100px (in css). The width is automated to keep the images in proportion but how do I get the width with jquery/js if I haven't specifically set it my self?

This is what I have tryed:

1) $(this).width();

2) $(this).css("width");

Both not working, any ideas?

Upvotes: 0

Views: 84

Answers (3)

Josh
Josh

Reputation: 2895

Try using this:

$(window).on('load', function() {
var x = 0;
$("img").each(function () {
x += $(this).width();
});
    alert(x);
});

Fiddle: http://jsfiddle.net/KFhG8/

Upvotes: 2

David Allen
David Allen

Reputation: 1163

The best way is to use maths and calculate it if you are struggling.

mynewwidth =  ( $(this).width() /  $(this).height() ) * mynewheight;

But it should work anyway see my fiddle

http://jsfiddle.net/davidja/KFhG8/4/

$(window).on('load', function () {


    mynewheight = 50;
    $("img").height(mynewheight);   

    // via $("img").width()
    alert( $("img").width());

    // maths
    alert(( $("img").width() /  $("img").height() ) * mynewheight)
});

Upvotes: 0

Vignesh Subramanian
Vignesh Subramanian

Reputation: 7289

Use clientWidth for that

var width=document.getElementById('imageid').clientWidth;

Upvotes: 1

Related Questions