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

My JavaScript code is as follows: $(document).keydown(function(event){ var move, inter; clearInterval(inter); inter = setInterval(move
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
Javascript Code YAHOO.util.Event.onDOMReady(function (ev) { var carousel = new YAHOO.widget.Carousel(container, {isCircular: true, numVisible:1}); console.log(carousel.set('navigation',
This Javascript code is using the 'this' keyword inside a nested function (which is
My javascript code is like this: function openWindows() { if(A is true) { window.open(URL_A);
On load I'm both calling a JavaScript setTimeout() function that will hide a .NET
I am trying to refer a external javascript file by calling a function in
I'm getting a function not defined Javascript error with the following code, which uses
When calling a Javascript function, it seems like JS gives priority to functions without
I'm having trouble calling a function through the use of ExternalInterface. First off, here's

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.