1

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"
}}]

2 Answers 2

4

Working fiddle.

.one() : Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

You could use JQuery one() that will trigger the click one time :

$("#add-order").one("click", function(){ 
    $.getJSON('API/orders.json', function(data){

        var my_orders = $("#myorders");

        $.each(data, function(i, order){
            my_orders.append("<li> id: " + data[i].order.id + "</li>");
            my_orders.append("<li> Name: " + data[i].order.name + "</li>");
            my_orders.append("<li> Drink: " + data[i].order.drink + "</li>")
        });    
    }); 
});

NOTE : You append invalid HTML, <li> tag should be a direct child of ul so you should remove p.

Hope this helps.


var data = [{ "order":{"id": "1",
    "name": "Bill",
    "drink": "Capuccino"}},


  { "order":{"id": "2",
    "name": "Sofia",
    "drink": "Late"
}}]

$("#add-order").one("click", function(){ 
  var my_orders = $("#myorders");

  $.each(data, function(i, order){
    my_orders.append("<li> id: " + data[i].order.id + "</li>");
    my_orders.append("<li> Name: " + data[i].order.name + "</li>");
    my_orders.append("<li> Drink: " + data[i].order.drink + "</li>")
  });   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="myorders"></ul>
<button type="button" id="add-order">Load the orders</button>

Sign up to request clarification or add additional context in comments.

3 Comments

I agree with your answer, and the html errors. 'p' tag is not needed and an can cause spacing errors. In side the li tag he can only use the 'a' tag and the span tag, and I do not see a need for them. The most he could add is a ul wrapper.
Happy to help @Zakaria. I Like the final results, clean and simple. Using the ul tag for DOM tracking is clever.
Thanks a lot Zakaria for correcting my post. I removed the <p> and replaced it with a <br /> in 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.
0

I 've just found out that there is a matter with that solution...if you change/update the data of the json file, then you have to reload the page because the one() has been already used once.

2 Comments

You did not mention that this was part of an ajax routine. Now .one() has to be reset after use. In other words a page change must reset the .one() function without a page refresh. I have been searching but not much detail on that function, unless it is implicitly a non-ajax compatible function.
I refferred to getJSON so I supposed u understand I was talking about ajax request. Now about your thought... interesting one, may be difficult in implementing may not... don't know... if you acquire any piece of info about it, please let me know. Thanks Sparky256

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.