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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:56:25+00:00 2026-05-11T13:56:25+00:00

Part of the jQuery/ajax asynchronous callback stuff still has me baffled, and I’m sure

  • 0

Part of the jQuery/ajax asynchronous callback stuff still has me baffled, and I’m sure it’s just because I don’t know javascript well enough.

Simplifying the code down as much as possible, this is where I’m stuck:

If I create an empty div with an id of ‘queuediv1’ I can fill it with the results of a page method like this.

$(document).ready(function() {      $.ajax({        type: 'POST',        contentType: 'application/json; charset=utf-8',        dataType: 'json',        url: 'test12.aspx/GetHtmlTest',        //data: '{ 'Dividend': '' + $('#Dividend').val() + '' }',        data: '{}',        // Error!        error: function(xhr, status, error) {            // Boil the ASP.NET AJAX error down to JSON.            //var err = eval('(' + xhr.responseText + ')');             // Display the specific error raised by the server            //alert(err.Message);            alert('AJAX Error!');        },         success: function(msg) {          $('#queuediv1').removeClass('isequeue_updating');          $('#queuediv1').html(msg);              //an unorderd list with the tag 'browserxx' was just inserted into the div          $('#browserxx').treeview();        }      });  });  

This works great; it doesn’t the block, and it gives me full control of error handling. However, when I try to expand this, I get into trouble. If I have several areas of the page that I want to update, I can alter the call so that each async call is made with the correct ‘data’, but I can’t tell the callback the id of the control that I want it to populate.

Simplifying my case down to something that is still broken:

Suppose there are 4 divs in the DOM that with id’s queuediv1,queuediv2,queuediv3, and queuediv4 that I want to update. I would like to reuse as much code as possible. While the number and id’s of divs to be updated will actually be dynamic, I thought this would have worked:

$(document).ready(function() {  for (i= 1;i<=4;i++)  {       var divname ='#queuediv'+i;       $.ajax({        type: 'POST',        contentType: 'application/json; charset=utf-8',        dataType: 'json',        url: 'test12.aspx/GetHtmlTest',        // data would be populated differently so that each div gets its own result- for now it doesn't matter        //data: '{ 'Dividend': '' + $('#Dividend').val() + '' }',        data: '{}',        // Error!        error: function(xhr, status, error) {            // Boil the ASP.NET AJAX error down to JSON.            //var err = eval('(' + xhr.responseText + ')');             // Display the specific error raised by the server            //alert(err.Message);            alert('AJAX Error!');        },         success: function(msg) {          $(divname).removeClass('isequeue_updating');          $(divname).html(msg);          $('#somethingfromthemsg').treeview();        }      });  }  });  

But this can’t ever work since by the time success is called the scope is wrong and the divname already equals ‘#queuediv4’ for every callback. Only that div gets updated (4x). Is there a way to pass a variable to the callback? Or am I just thinking about the problem wrong.

I did find something like this dealing with async calls to $.getJSON here: http://thefrontiergroup.com.au/blog/tag/jquery

This site talked about wrapping the callback inside another anonymous function to preserve the calling variables. That sort of made sense for scope but I have no idea how to form that the way the $.ajax call is created.

  • 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. 2026-05-11T13:56:25+00:00Added an answer on May 11, 2026 at 1:56 pm

    You can wrap every iteration of the for loop in an anonymous function like this:

    $(document).ready(function () {     for (i = 1; i <= 4; i++) {         (function () {             var divname = '#queuediv' + i;             $.ajax({                 type: 'POST',                 contentType: 'application/json; charset=utf-8',                 dataType: 'json',                 url: 'test12.aspx/GetHtmlTest',                 data: '{}',                 error: function (xhr, status, error) {                     alert('AJAX Error!');                 },                 success: function (msg) {                     $(divname).removeClass('isequeue_updating');                     $(divname).html(msg);                     $('#somethingfromthemsg').treeview();                 }             });         })();     } }); 

    A much simpler example would be:

    <div id='output1'></div><div id='output2'></div><div id='output3'></div><div id='output4'></div><div id='output5'></div> <script language='javascript'>     for (var a = 1; a <= 5; a++) {         (function () {             var divName = 'output' + a;             var b = a;             setTimeout(function () { document.getElementById(divName).innerHTML = b; }, 2000);         })();     } </script> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm loading part of my webpage using AJAX, in particular jQuery.load() . With this
I'm prepending the markup returned after an jquery ajax() call [the success part of
I have a form I'm submitting via ajax with jQuery, it's part of a
Possible Duplicate: What does Asynchronous means in Ajax? jQuery ajax return value Trying to
Possible Duplicate: jQuery Ajax request every 30 seconds I know we can load a
I am using jQuery $ajax call to update some part of the page. However,
I'd expect a right-click-context-menu-style pop-up menu to be a part of jQuery UI. But
We have this anonymous function in our code, which is part of the jQuery's
I'm using jQuery .append() to build part of a website with data I get
Here's part of my code for a jQuery word counter. My question is: how

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.