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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:06:50+00:00 2026-06-14T19:06:50+00:00

I tried to load an image into an HTML element using JQuery ad use

  • 0

I tried to load an image into an HTML element using JQuery ad use the image properties to do other things.
What i tried to do is something like this:

var img = new Image();
img.src = "icon.png";
var width = img.width;
var height = img.height;
$("#element").css("background-image", "url(" + img.src + ")");
//use width and height...

It works most of the times but i found that sometimes “width” and “height” are set to 0.
Doing some searches I found that the image load is an asynchronous job, so it happens that i try to read the properties when it is still not loaded.
I found that there is an onLoad event that i can use to specify a callback function when the image is loaded.

I wonder if it is the best way to do such things. Let be the case, for example, that I only need some information about an image but i don’t need to display it, and i want to manage that information client side.
Do i need to load the image?
Is there some other way to obtain such information?

  • 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-14T19:06:52+00:00Added an answer on June 14, 2026 at 7:06 pm

    You have to wait for the image to be loaded:

    var img = new Image();
    img.onload = function() {
      var width = img.width;
      var height = img.height;
      $("#element").css("background-image", "url(" + img.src + ")");
    };
    img.src = "icon.png";
    

    Because the image has to be fetched from the URL assigned to the “src” attribute, the operation is treated asynchronously. That is, you’re not guaranteed that the image will be available for the statement following the assignment of the “src” attribute.

    It works sometimes because your image may be cached.

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

Sidebar

Related Questions

I tried to load my Array s like this, but it loads just first
I tried to load an image into my WPF project's main window. First I
I am using jquery ajax to load a separate page into a modal window
I have an Android Webview where I load HTML into using loadDataWithBaseURL, some of
I tried to load top3 record of table into DataGrid it shows error Cannot
I tried the following: $.load(Views/chatBox.html).appendTo('body') Console Output: TypeError: $.load is not a function EDIT
My jQuery code (using ajax) request's data from a local php script (pgiproxy.php). This
Good evening everyone, I am using a JavaScript to load/override content from an HTML-File
I have a picturebox where i can load a image into its .backgroundimage function
I want to load an RGB image in MATLAB and turn it into a

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.