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

  • SEARCH
  • Home
  • 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 7188595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:07:08+00:00 2026-05-28T19:07:08+00:00

I’m trying to write some jQuery code that gets the combined width of images

  • 0

I’m trying to write some jQuery code that gets the combined width of images that have been cloned and resized before they are shown, the problem is that before they are added to the DOM the width is always 0.

(function($){
    var $images = createDiv(),
        totalWidth = 0;

    //loop through a list of images
    $("#images > li > img").each(function(){
        var $this = $(this),
            $img = $this.clone(); //createa  duplicate of the images

        $img.height(100); //resize each duplicate to a set height

        totalWidth += $img.width(); //always equals zero

        //this always results in 0 too 
        //totalWidth += $img.get(0).clientWidth; 

        //add the cloned image to the div
        $images.append($img);

        //remove full sized image to reattach later
        $this.remove();
    });

    $images.hide();

    $("body").append($images);

    $("#show").on('click',function(){
        $images.show();
    });

    function createDiv(){
        return $('<div id="imgs"></div>');
    }
}(jQuery));

Here is a fiddle of a complete example: http://jsfiddle.net/bittersweetryan/9c3SH/2/

How can I get the total combined width of the resized images before they are added to the DOM?

  • 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-05-28T19:07:09+00:00Added an answer on May 28, 2026 at 7:07 pm

    You first need to append the image to the DOM, for it to have a width attribute, as an unattached DOM node it has no ‘physical’ properties. With that said, I suggest this:

    (function($){
        var $images = createDiv(),
            totalWidth = 0;
    
        //loop through a list of images
        $("#images > li > img").each(function(){
            var $this = $(this),
                $img = $this.clone(); //createa  duplicate of the images
    
            $img.height(100); //resize each duplicate to a set height
    
            /* the call to css() is to make sure the created image isn't visible
               in the window. */
    
            $img.css(
                {
                    'position': 'absolute',
                    'left' : -10000 + 'px',
                });
    
            /* appending the element to the body */
    
            $img.appendTo($('body'));
    
            totalWidth += $img.width(); //always equals zero
            //this always results in 0 too 
            //totalWidth += $img.get(0).clientWidth; 
    
    
            /* just to be sure... */
    
            console.log(totalWidth);
    
            /* *moving* the created image from its previous place off-screen,
               and removing also the 'position: absolute;' so that it's visible */
            $img.css({'position' : 'static'}).appendTo($images);
    
            //remove full sized image to reattach later
            $this.remove();
        });
    
        $images.hide();
    
        $("body").append($images);
    
    
        $("#show").on('click',function(){
            $images.show();
        });
    
        function createDiv(){
            return $('<div id="imgs"></div>');
        }
    }(jQuery));
    

    JS Fiddle demo.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.