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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:21:52+00:00 2026-05-17T21:21:52+00:00

In my JavaScript code I am first calling a function to update an image:

  • 0

In my JavaScript code I am first calling a function to update an image:

document.getElementByID("i1").src = newImage;

then a few statements later the alert() function is called to display a message:

alert("image updated!");

However, when I run this code, what actually happens is that the alert box is popped up before the image is updated. When I click the “OK” button in the dialog to take the alert box down, the image updates.

Why is the order of these events not being preserved? And is there some kind of synchronizing function I can call to wait for the image update to complete before the alert dialog is displayed?

I’m open to restructuring the code in some ways (i.e., using something other than the alert() function), but I’d prefer a solution that allows the existing code to work as intended.

  • 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-17T21:21:52+00:00Added an answer on May 17, 2026 at 9:21 pm

    Image loading is asynchronous. What you need is a callback for when the image is loaded.

    If you are using jQuery, you can do it like this:

    var img = $('#i1');
    img.load(function() {
        alert('image updated!');
    });
    img.src = newImage;
    

    If you are using Prototype, you can do it this way:

    var img = $('i1');
    Event.observe(img, 'load', function(e) {
        alert('image updated!');
    });
    img.src = newImage;
    

    Note that in either case it’s important that you first define the callback, then set the image src.

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

Sidebar

Related Questions

HI all, Below mentioned javascript code works fine in all browsers including chrome(from second
If I pass a ClientID to a javascript function as a parameter without including
I'm working with some existing javascript code which takes an LI and makes it
I have the following code that I am playing with: <script type=text/javascript> var Dash
I was toying (read: learning) around with Javascript and came across something to my
First off, I know this is a base JS issue, not jQuery. I am
I have two XML sources to retrieve data from. I want to use them
My problem is related to using the JQuery Tools Tooltip Plugin ( http://flowplayer.org/tools/tooltip.html )
I have one question; In my ASP.NET MVC web application have to do certain
I'm creating a game in XNA and was thinking of creating my own scripting

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.