1

I am trying to grab three different json files that contain objects and load them into a single page using the jquery get method. I need to grab the objects and display all three to the page using jquery. I am having trouble loading and displaying all three... any help is appreciated. Thanks!

Here is the code:

$(document).ready(function(){
$("button").click(function(){
    $.get("objectone.json", function(data){
        $('#mydiv').html((JSON.stringify(data)));
    });
    $.get("objectwo.json", function(data){
        $('#mydiv').html((JSON.stringify(data)));
    });
    $.get("objectthree.json", function(data){
        $('#mydiv').html((JSON.stringify(data)));
    });
  });
});

and the HTML:

<button type="button" name="button">Click Me</button>

<div id="mydiv">

</div>

2 Answers 2

3

The .html method replaces all of the contents of the selected element with the html contents you provide. By using it three times on the same element you replace all of the contents three times.

Replace .html with .append, which adds the new data to the end of the contents of the selected element.

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

4 Comments

Gotcha! Is there anyway to stop the function from running when all three objects appear on the page? Because everytime I click the button to run the function another set of the same objects appear. Anyway to keep that from happening?
@MujoM You can either remove the button or stop it from working. To remove it: $("button").remove(); and to stop it from working: $("button").unbind('click'). Just add one of the above at the end of the "click" callback (one line before the last one)
@MujoM you can also add a class to the element to indicate that it has been filled. At the end of the callback function: $("mydiv").addClass('filled'); and in the beginning ad an "if" with if (!$("mydiv").hasClass('filled') { the rest of the code here }
Awesome! Thanks for the help!
2

Instead of .html() method use .append() of JQuery.

Since you are using .html() it will replace the previous content with the new content whenever you call it, whereas with .append() it will add to the exisiting content.

Read More here

$(document).ready(function() {
  var url = "https://rawgit.com/typicode/json-server/master/routes.json";
  $("button").click(function() {
    $.get(url, function(data) {
      $('#mydiv').append((JSON.stringify(data)));
    });
    $.get(url, function(data) {
      $('#mydiv').append((JSON.stringify(data)));
    });
    $.get(url, function(data) {
      $('#mydiv').append((JSON.stringify(data)));
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" name="button">Click Me</button>

<div id="mydiv">

</div>

Comments

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.