Rob
Rob

Reputation: 15992

How can I count the total width of many images in a div?

I have a div element like this:

<div id="test">
    <img src="..."/>
    <img src="..."/>
    <img src="..."/>
    <img src="..."/>
</div>

What I want to do is first to make all the images having the same height, then calculate the sum of their new widths. I tried this :

window.onload = function() {
    var width = $(window).width(), totalwidth=0;
    $('#test img').height('200px');
    $("#test").each(function() {
        totalwidth += $("#test img").attr("width");
  });
  alert(totalwidth);
}

But the alert returns NaN. What should I do ?

Upvotes: 0

Views: 420

Answers (1)

Tamil Selvan C
Tamil Selvan C

Reputation: 20199

Try

 $("#test img").each(function() {
        totalwidth += $(this).width();
  });

Upvotes: 6

Related Questions