mysterious problem with JS variable / variable is displayed only after alert
Hello,
I have a big problem with the "output2"-variable!
output2 is declared in the first ajax query(line 15) and is described in the second ajax query(line 30 and 32).
Now I want to use output2(line 45), but the variable is empty!
Now the mystery:
I write a alert() befor line 45 and the variable is filled.
WHY?
- $("#AJPizza").bind("click", function() {
- $.ajax({
- type: "POST",
- url: "r_article.php",
- dataType: "xml",
- success: function(xml) {
- var output = '';
-
- $('articleZ', xml).each(function(i) {
- var idA = $(this).find("idA").text();
- var nameS = $(this).find("nameS").text();
- var number = $(this).find("number").text();
- var nameA = $(this).find("nameA").text();
- var picture = $(this).find("picture").text();
- var output2 = '';
-
- $.ajax({
- type: "POST",
- url: "r_ingredient.php",
- data: {idArticle: idA},
- dataType: "xml",
- success: function(xml2) {
- var number = $('ingredientZ', xml2).length;
- $('ingredientZ', xml2).each(function(k) {
- var idI = $(this).find("idI").text();
- var name = $(this).find("name").text();
- number --;
- if(number != 0) {
- output2 += name + ", ";
- } else {
- output2 += name;
- }
- });
- }
- });
-
- //alert(idA);
-
- output += '<div data-role="collapsible">';
- output += '<h3>' + nameA + ' (' + nameS + ')</h3>';
- output += '<ul data-role="listview">';
- output += '<li>';
- output += '<img src="pizza.jpg" />';
- output += '<p>Belag: ' + output2 + '</p>';
- output += '</li>';
- output += '</div>';
- });
- $("#coll_article").html(output);
- $("#tester").trigger('create');
- }
- });
- });