Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:12:07+00:00 2026-06-12T23:12:07+00:00

this is slightly related to this question Invoking a jQuery function after .each() has

  • 0

this is slightly related to this question
Invoking a jQuery function after .each() has completed

But the solution doesn’t quite seem to work for me.

Basically I have piece of JavaScript that calculates the width of some elements:

var totalWidth = 0;
$element.find('li').each(function () {
    totalWidth += $(this).width();
});

After this I want to then use the width to set another element:

$element.width(totalWidth);

If I attach the debugger it works but if I just run it it doesn’t. Which makes me think that the width is being set before the .each() completes.

I tried using .promise() to fix this issue but it doesn’t appear to make a difference.

var totalWidth = 0;
$element.find('li').each(function () {
    totalWidth += $(this).width();
});

$element.promise().done(function () {
    $element.width(totalWidth);
});

Here’s my HTML

<div class="carousel">
        <ul>
            <li>
                <img src="/Images/Travel2New/Adverts/advertHolder_1.png" />
            </li>
            <li>
                <img src="/Images/Travel2New/Adverts/advertHolder_2.png" />
            </li>
            <li>
                <img src="/Images/Travel2New/Adverts/advertHolder_3.png" />
            </li>
            <li>
                <img src="/Images/Travel2New/Adverts/advertHolder_4.png" />
            </li>
            <li>
                <img src="/Images/Travel2New/Adverts/advertHolder_5.png" />
            </li>
        </ul>
    </div>

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:12:08+00:00Added an answer on June 12, 2026 at 11:12 pm

    Since your width calculation should wait until the graphics is loaded then you can try the following:

    $(window).load(function () {
        var totalWidth = 0;
    
        $element.find('li').each(function () {
            totalWidth += $(this).width();
        });
    
        $element.width(totalWidth);
    });
    

    From load() event example description we can see:

    Run a function when the page is fully loaded including graphics.

    $(window).load(function () {
        // run code
    });
    

    Note that at the moment the load() event seems to be deprecated, so you should consider the version of jQuery you exploit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been done but I have a slightly different twist
Slightly related to this question , but you don't really need to read that.
Related to this question, but slightly different and hopefully more clear. I am looking
This is perhaps related to this question , but I have slightly more information.
This is a slightly tricky question. I am using NSDateFormatter on the iPhone but
Apologies if this is a slightly noob question, but looking to clarify my thoughts
Although slightly related to a previous question, it is different. How secure is this
I realize that this question is slightly server related and could be posted on
This is slightly related to the topic covered in a question about allowing an
I realize Running PhantomJS on Heroku is a related but slightly different question as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.