I want from the following code to retrieve the data of the json file but the problem is that if I press again the button I see twice the results. I changed the first append of my each loop to html but then only the last object of my json is loaded.
HTML :
<ul id="myorders"></ul>
<button type="button" id="add-order">Load the orders</button>
JS :
$("#add-order").click(function(){
$.getJSON('API/orders.json', function(data){
$.each(data, function(i, order){
$("#myorders").append("<p><li> id: " + data[i].order.id + "</li>");
$("#myorders").append("<li> Name: " + data[i].order.name + "</li>");
$("#myorders").append("<li> Drink: " + data[i].order.drink + "</li></p>")
});
});
});
Data example :
[{ "order":{"id": "1",
"name": "Bill",
"drink": "Capuccino"}},
{ "order":{"id": "2",
"name": "Sofia",
"drink": "Late"
}}]
and replaced it with a
– Danis35 Apr 01 '16 at 23:02in the end of loop so the second object of the json file to be separated from the first one in the output. I also used the one() instead of bind () that is the "usual" one in most cases. It worked! Thank you!! Did n't know about one(). Know now... Thank you Sparky for your comment, too.