0

little confused, i have a .each function which displays all results from JSON when i use console.log but when i try to output using .html() it only shows one? any reason why?

code:

$(document).ready(function(){
    $.get('functions/ListOrders.php', function(xml){ 
        var newOrders = $.xml2json(xml);
        $.each(newOrders.ListOrdersResult.Orders.Order, function(index, value) { 
            //console.log(value.AmazonOrderId);
            $('#orderAmount').html("<b>Order Total:</b><br>" + index + "<br><br>");
            $('#orderListing').html("<b>Order Listing:</b><br>" + value.AmazonOrderId);
        });
    });
});

Thanks

5
  • 5
    You're overwriting the contents of a single element every time. Commented May 14, 2014 at 1:42
  • how do i resolve it? sorry im new to this ;) Commented May 14, 2014 at 1:46
  • 1
    Try replacing .html() with .append(). Fiddle with it from there. Commented May 14, 2014 at 1:46
  • 1
    Downvoting the question is unnecessary btw. He's just new and doesn't know... he asked the question properly. I upvoted to counter the downvote. Commented May 14, 2014 at 1:46
  • Thanks Hamza, append was the solution! thanks! Commented May 14, 2014 at 1:53

1 Answer 1

1

You getting only one result because in your loop you override existing value. Basically you override html value. If you use append instead it will add values to your existing elements with each loop iteration.

$(document).ready(function(){
    $.get('functions/ListOrders.php', function(xml){ 
        var newOrders = $.xml2json(xml);
        $.each(newOrders.ListOrdersResult.Orders.Order, function(index, value) { 
            $('#orderAmount').append("<b>Order Total:</b><br>" + index + "<br><br>");
            $('#orderListing').append("<b>Order Listing:</b><br>" + value.AmazonOrderId);
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

+1, but just add him an explanation why he is getting only on result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.