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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:58:33+00:00 2026-06-06T12:58:33+00:00

Rather odd problem in that I cannot use the data variable (the information returned

  • 0

Rather odd problem in that I cannot use the data variable (the information returned by the ajax call) anywhere but in the .ajax function itself.

I am sure this is an issue of scope, however it is one that is beyond me and would be grateful of any pointers.

$('img#test').live('click', function(e) {
    e.preventDefault();
    var test = getPreviewImage();
    alert(test); // This just gives undefined
});


function getPreviewImage()
{
  var output;

  var img_bg = $('div#preview-1 img:nth-child(1)').prop('src');
  var img_fg = $('div#preview-1 img:nth-child(2)').prop('src');


  $.ajax({
    url: "/blah.php?v=12345,

  }).done(function (data) {

    alert(data); // This gives the correct response
    output = data; // This should take the data value but still be in scope for the return statement below

  });

return output;
}
  • 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-06T12:58:34+00:00Added an answer on June 6, 2026 at 12:58 pm

    This isn’t really a problem of scope but of synchronicity.

    When your getPreviewImage function returns, the ajax call hasn’t yet be made (it’s asynchronous and the execution flow doesn’t wait for the request and response to be complete), so output is still null.

    You can solve this by making a synchronous ajax call or by providing a callback to getPreviewImage instead of using its return value.

    To make a synchronous ajax call, pass false as the async parameter. See the doc.

    To use a callback, you can do this :

    $('img#test').live('click', function(e) {
        e.preventDefault();
        getPreviewImage(function(test){
            // use test
        });
    });
    
    
    function getPreviewImage(callback) {
    
      $.ajax({
        url: "/blah.php?v=12345",...
    
      }).done(function (data) {
        callback(data);
      });
    }
    

    Using a synchronous call is easier (you just have to set a parameter to false) but the callback logic is generally preferable as it doesn’t block your script and allows parallel requests.

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

Sidebar

Related Questions

I am having a rather odd problem with the Gecko Webbrowser control, I have
I am facing a rather odd problem with our groovy build script. The whole
I have some rather odd behavior in my D program that I've narrowed down
I realize this is a rather odd request, but I was wondering if anyone
My PHP experience is rather limited. I've just inherited some stuff that looks odd
I have a rather odd problem: I need to maintain a chunk of code
I have a rather odd problem with Doxygen (1.6.1 on Mac OS X Snow
Yesterday I discovered an odd bug in rather simple code that basically gets text
It seems rather odd that I can't figure how to do this in mustache.
I've just come across a rather odd problem whilst testing my applications webflows. I

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.