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 8031399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:05:26+00:00 2026-06-05T01:05:26+00:00

Curious problem. I have a set of images with some attributes that I’d like

  • 0

Curious problem. I have a set of images with some attributes that I’d like to use in a div (as a caption). Using each, I want to get the width of the image (and some other properties), to align the dynamically generated div properly (as a caption of the image).

img = $('img') 
img.each(function(index){
    var width = $(this).width();
    console.log(width);
    // other properties
    $('<div class="caption">Some text</div>').insertAfter($(this));

$(this).next().css({
  'width': width
  // other properties
});

However, sometimes $(this).width() gets the right value, other times gets 0. It’s particularly well behaved when a press return in the direction bar but not when I press Ctrl+R but only in Chrome. It doesn’t work the same in all browsers, so it’s a mess. I thought that Jquery was attempting to retrieve width() before the image was loaded, so I wrap my code in document.ready(function(){}) instead of (function(){})() but it doesn’t work either way.

What could be happening?. Just as a reference, these are the styles applied to the images:

display: block;
padding: 4px;
line-height: 1;
max-width: 100%;
margin-left: auto;
margin-right: auto;

So I’d like to get the computed width (which, as far as I know, should be retrieved by .css(), but in this case, not consistently).

Regards

  • 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-05T01:05:27+00:00Added an answer on June 5, 2026 at 1:05 am

    More than likely the images aren’t completely loaded all the time when you run that code. Try ensuring that the images are done loading first.

    function imgDone(){
        var width = $(this).width();
        console.log(width);
        // other properties
        $('<div class="caption">Some text</div>').insertAfter($(this));
    }
    $('img').each(function(index){
        if (this.complete || this.readyState = 4)
            imgDone.apply(this);
        else
            $(this).load(function(){
                imgDone.apply(this);
            });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to have a curious problem. I set up OpenGL like this: glViewport(0,
I have a div tag which I populate dynamically. The problem is that in
I have a rather curious problem, using the XElement load method to load in
My problem is that I have to set a variable in a try statement
I have a curious problem with IE, IIS 6.0 dynamic PNG files and I
I have a curious problem: My app is distributed through ClickOnce. I recently installed
I don't have a problem, I'm just curious. Here is a part of my
I'm curious how others have solved this problem, and what problems might lurk behind
Just curious. I heard something about the new G1GC addressing that latency problem. I
I had a problem that was making the original array to change, the curious

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.