1

thanks to some help i got around i made this jquery function that gets the sum of widths of IMGs within a DIV.

now i have a new problem: multiple div.IDs with different widths, how can i make it most performant?

my code is this, for 1 div:

$(window).load(function(){
var widthSum = 0;
$('#oneid.scroll-content-item img').each(function(){
widthSum += $(this).width() + 20;
});
$( ".scroll-content" ).css('width', widthSum);

should i repeat this code several times with different IDs? if i make for example multiple selectors like this:

$('#oneid.scroll-content-item img, #twoid.scroll-content-item img').each(function(){

it will count them together, but i need separate.

any ideas?

thanks all!

3
  • Why don't you just get the width of whatever HTML element contains all the images? If there isn't one, put them in there. That way you can avoid having to fudge things (like with the + 20). Commented Dec 6, 2012 at 23:42
  • It seems to me you're using an id like you should be using a class. Commented Dec 6, 2012 at 23:43
  • Example of using a container width: jsfiddle.net/eQXug Commented Dec 6, 2012 at 23:48

2 Answers 2

2
//this will go over any element with the class "scroll-content-item" seperately
$('.scroll-content-item').each(function(){
    var wrapper = $(this);
    var wrapperWidth = 0;

    //this will go over every img inside the seperate wrappers
    wrapper.find('img').each(function(){
        wrapperWidth += $(this).width() + 20;
    });

    wrapper.css('width', wrapperWidth);
});
Sign up to request clarification or add additional context in comments.

5 Comments

hi! thanks, i'm using out this one now, trying to make it work. from what i understand the .width() + 20 is for the margin, should I use something like outerWidth() and remove the +20? it's almost 100% working
you could try using .outerWidth(true), yes
tried that and + 20 together and i get a blank space after last img. if i remove +20 then the last image jumps to a 2nd row (on one div) and fits good on the other div ?!
hard to say without seeing an example what's the case here. can you set up something at jsfiddle.com?
yes, thats understandable. i'm using wordpress, would be a pain to move to jsfiddle. one demo site works perfect with one DIV. let me give it a few tries, maybe i can solve it on my own. anyways except the little blank space after last element, works perfectly! thank you!
0
function getWidth(elem) {
    var widthSum = 0;
    $('img', elem).each(function(){
        widthSum += $(this).width() + 20;
    });
    return widthSum;
}


$(window).load(function(){
    $('.scroll-content').each(function() {
        $(this).css('width', getWidth(this));
    });
});
​

1 Comment

still the same, no value returned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.