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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:17:46+00:00 2026-05-13T00:17:46+00:00

Probably a simple solution but I’m stuck and it’s late… :) $j(‘#thisImage’).html(‘<img src=image.jpg id=box

  • 0

Probably a simple solution but I’m stuck and it’s late… 🙂

$j('#thisImage').html('<img src="image.jpg" id="box" />');
var imgWidth = $j('#box').width();

When I do a console.log(imgWidth) it’s returning 0…

I’m pretty sure that it’s because the .html(...) is not fully finished before the width(...) call is fired. I’ve tested it out with cached images and it works fine and gives me the correct image width… yet those that aren’t cached are returning 0.

So I’m looking for a good solution that waits for the .html(...) call to finish before moving on to my .width(...) call

Any help is greatly appreciated. Thx!


UPDATE 1

Using CMS answer below… (and this may help everyone understand my dilemma)

If I do this:

var imgWidth = 0;

$j('<img src="image.jpg" id="box" />').appendTo("#thisImage").load(function() {
    imgWidth = $j('#box').width();

    //my first log
    console.log("Width 1: " + imgWidth);                
});             

//my second log
console.log("Width 2: " + imgWidth);

if (imgWidth > 100) {
    //do something
}

Then //my second log is returning before //my first log as the .load hasn’t finished completing…

thus my if statement is always false as imgWidth is always 0…

that is until .load completes… but then it’s too late…


UPDATE 2 : SEE WHAT I SEE

Using a modification of Ryan’s answer…

Go to jsbin.com -> Include “jquery” from drop down menu -> Replace w/ below in the Javascript Tab and then click the Output Tab

function loadImg(url, func) {
    var img = new Image();
    img.onload = func;
    img.src = url;
    return img;
}

$(document).ready(function() {
    var myWidth = 0;

    var myImg = loadImg("http://sstatic.net/so/img/logo.png", function() {
                    myWidth = $(this).width();
                    $('#hello').append("Show Me First: " + myWidth + "<br />");
                });


    $(myImg).appendTo("body");

    if(myWidth > 0) {
        $('#hello').append("Success!");    
    } else {
        $('#hello').append("Show Me Second: " + myWidth + " <- BOO! Out of Order! <br />");
    }

});
  • 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-13T00:17:46+00:00Added an answer on May 13, 2026 at 12:17 am

    Try something like the code below:

    function loadImg(url, func) {
        var img = new Image();
        img.onload = func;
        img.src = url;
        return img;
    }
    
    var myImg = loadImg("lulz.jpg", function() {
        console.log($j(this).width());
    });
    
    $j(myImg).appendTo("#thisImage");
    

    This pretty much does the following:

    • Sets the image to load in the background, handing it off to the browser
    • Allows jQuery to go ahead and construct the DOM element, append it, etc
    • Our custom loadImg function accepts a callback function that fires when the image is ready

    Of course, it goes without saying that you wanna do this after the DOM is ready, so document.ready is your friend and all that jazz. Have fun. ;D

    Edit, in response to OP’s edits:

    Your code is as follows:

    function loadImg(url, func) {
        var img = new Image();
        img.onload = func;
        img.src = url;
        return img;
    }
    
    $(document).ready(function() {
        var myWidth = 0;
    
        var myImg = loadImg("http://sstatic.net/so/img/logo.png", function() {
                        myWidth = $(this).width();
                        $('#hello').append("Show Me First: " + myWidth + "<br />");
                    });
    
    
        $(myImg).appendTo("body");
    
        // This section is problematic...
        if(myWidth > 0) {
            $('#hello').append("Success!");    
        } else {
            $('#hello').append("Show Me Second: " + myWidth + " <- BOO! Out of Order! <br />");
        }
    });
    

    I commented the problematic section; the image loading is an asynchronous thing. By the time that block is hit, there’s no guarantee that your image will be ready. Perhaps I should’ve specified this in my earlier answer, but in this case it’s better to do all your DOM manipulation in the image’s callback function, so it fires when the image is actually ready.

    Commented this rather than type another paragraph, but something like…

    // Move this outside the $(document).ready, for scoping reasons...
    var myWidth = 0;
    
    function loadImg(url, func) {
        var img = new Image();
        img.onload = function() { func(img); };
        img.src = url;
        return img;
    }
    
    $(document).ready(function() {
        loadImg("http://sstatic.net/so/img/logo.png", function(img) {
            var myImg = $(img);
    
            // Since you can't get the width until the image is appended, get around it
            // by throwing it in hidden; get the width, then unhide it and do what you want.
            myImg.css({
                "position": "absolute",
                "visibility": "hidden"
            }).appendTo("body");
    
            myWidth = myImg.width();
    
            myImg.css({
                "position": "normal",
                "visibility": "visible"
            });
    
            if(myWidth > 0) {
                $('#hello').append("Success!");    
            } else {
                $('#hello').append("Show Me Second: " + myWidth + " <- BOO! Out of Order! <br />");
            }
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably a long question for a simple solution, but here goes... I have a
This is probably a simple question but I can't seem to find the solution.
There is probably a simple solution but I can't figure it out. I am
there's probably a very simple solution but I'm at my wit's end here. I
New Rails programmer here. There is probably a pretty simple solution to this, but
The solution to this is probably very simple, but I'm not sure what I'm
There is probably a simple solution to this question, but if i have the
Probably a simple solution, but i don't get it working. I have a table
I'm new with Objective-C, so there probably is a simple solution to this. I
There is probably is simple fix for this but I currently have code similar

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.