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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:29:23+00:00 2026-05-18T09:29:23+00:00

I use $.ajax to read xml info in for loop here is my xml

  • 0

I use $.ajax to read xml info in “for” loop

here is my xml file:

<application>
    <app id="id-1">
  <html_code>
          <div id="id-1" class="portlet">
               <div class="portlet-header"Links</div>
               <div class="portlet-content">id-1</div>
          </div>
  </html_code>
   </app>
    <app id="id-2">
           <html_code>
                <div id="id-2" class="portlet">
                     <div class="portlet-header"Links</div>
                     <div class="portlet-content">id-2</div>
                </div>
           </html_code>
   </app>
<application>

then I use $.ajax to get the content in tags ,and prepend to a contain in this page
here is the js code:

$.ajax({
   ……
  success:function(){
       for (var i = 0; i < $children.length; i++) {                        
        var $temp_id = $children.eq(i).attr("id");  //"$children" have defined in above 
        $.ajax({
              type: "get",
              url: "Database/App_all.xml",
              dataType: "html",
              success: function (xml) {
                        var $temp_code = $(xml).find("app[id='" + $temp_id + "']").find("html_code").html();
                        $($temp_code).appendTo($("#contain")).hide().show('slow');
                         },
                          error: function () { }
                    });
             }    
  }
});

suppose the $children.length is only 2,so the resault is that in the contain <div id="contain"> there should have <div class="portlet-content">id-2</div> and <div class="portlet-content">id-1</div>

but the resault is that in <div id="contain"> only have one kind ,is <div class="portlet-content">id-2</div>

what’s wrong with this ?

but when I write the alert(“”); between for (var i = 0; i < $layout_left_children.length; i++) {andvar $temp_id = $layout_left_children.eq(i).attr("id");
like

  for (var i = 0; i < $children.length; i++) {
            alert($children.eq(i).attr("id"));        //could alert correct "id",first "id-1", then "id-2"                      
            var $temp_id = $children.eq(i).attr("id");  //"$children" have defined in above 

then the contain could be right as

So why could this happened? how can I solve this problem?

thank you:)

  • 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-18T09:29:24+00:00Added an answer on May 18, 2026 at 9:29 am

    I believe there are a couple of things happening. First, the $temp_id variable is getting hoisted to the top of the function, so it’s equivalent to doing this:

    $.ajax({
       ……
      success:function(){
           var $temp_id;
           for (var i = 0; i < $children.length; i++) {                        
              $temp_id = $children.eq(i).attr("id");
    

    Secondly, even though $temp_id is equal to “id-1” in the first loop, it gets changed to “id-2” in your second loop, so by the time your success callback is called for the first time, it’s already been changed to “id-2”.

    This should solve your problem:

    UPDATED 2010-12-03: fixed a bug

    $.ajax({
       ……
        success:function(){
             for (var i = 0; i < $children.length; i++) {                        
                var $temp_id = $children.eq(i).attr("id");  //"$children" have defined in above 
    
                $.ajax({
                    type: "get",
                    url: "Database/App_all.xml",
                    dataType: "html",
                    success: function($tid) {
                        return function (xml) {
                            var $temp_code = $(xml).find("app[id='" + $tid + "']").find("html_code").html();
                            $($temp_code).appendTo($("#contain")).hide().show('slow');
                        }
                    }($temp_id),
                    error: function () { }
                });
    
            }    
        }
    });
    

    What I’m doing is passing in $temp_id to the function that returns another function, which will be called in the success callback. It’s now safe to use $tid in your success callback because it won’t be impacted when $temp_id changes in the second loop.

    UPDATE: Response to hh54188’s comment below

    Using “alert” can break the execution process and allow ajax callbacks to be called unexpectedly. This is the case in IE and Firefox. Chrome on the other hand doesn’t behave like this. To test this, you can run this code:

    for (var i = 0; i < 2; i++) {
    
        //if (i == 1) alert('stopping execution');
    
        console.log('loop: ' + i);
        $.ajax({
            url: "Database/App_all.xml",
            dataType: "html",
            success: function () {
                console.log('callback');
            }
        });
    }
    

    You’ll see that the output in the console is:
    loop: 0
    loop: 1
    callback
    callback

    Now uncomment the line with the alert. In Firefox and IE, you’ll see that the output in the console is now:
    loop: 0
    callback
    loop: 1
    callback

    The first callback gets called while the alert box is displayed. The alert has essentially changed the behavior of your code. Using “alert” can be a source of frustration while developing in JavaScript because it can cause code execution to be unpredictable. Because of this, I don’t recommend the use of “alert” while debugging. Instead, it’s a lot more predictable to use console.log(). console.log() works in all modern browsers and IE 8+. For older IE, you’ll want to output text into the DOM.

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

Sidebar

Related Questions

I have written a web app in PHP which makes use of Ajax requests
I don't currently use ajax.net though I would be open to it if it
I don't want to use jQuery, but I'd like to use Ajax to do
Does excess use of AJAX affects performance? In context of big size web-applications, how
I am making use of AJAX on my site and I would like to
I am trying to use jQuery's ajax functionality to update data from a web
Is it possible to use a jQuery ajax call to perform Forms Authentication with
In the Ajax toolkit you can use a Tab Container and add TabPanels to
We use Sajax for adding small Ajax code to sites. After running into a
After the suggestion to use a library for my ajax needs I am going

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.