1

I have some basic markup:

<div id="container">
    <div class=".content"></div>
    <div class=".content"></div>
    <div class=".content"></div>
    <div class=".content"></div>
    <div class=".content"></div>
    <div class=".content"></div>
</div>

and the following jQuery code:

var listWidth = [];
    $('.content').each(function(){
        listWidth.push($(this).width());
                console.log(listWidth);
    });

This returns:

[200, 540, 200, 540, 200, 540]

How do I set the width of the parent element to the sum of the values within the array, so in this case 2220?

Many thanks in advance.

2 Answers 2

2

Try this

var width = 0;
$.each(listWidth, function(){
   width += this;
});

$("#container").width(width);
Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps something like this:

var divWidth = 0;
$(listWidth).each(function(){
    divWidth += Number(this);
});

$('#container').css('width',divWidth);

Comments

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.