0

I want to dynamically populate a list with elements from a JSON file. The idea is to switch the test_ID in the list with the actual object from the JSON file. How do I do this?

JSON file

 [
   {
     "id": "a",
     "test": "Java",
     "testDate": "2016-08-01"
   },
   {
     "id": "b",
     "test":"JavaScript",
     "testDate": "2016-08-01"
   }
 ]

jQuery

 $(function(){

  $.ajax({
    type : 'GET',
    url : 'json/data.json',
    async : false,
    beforeSend : function(){/*loading*/},
    dataType : 'json',
    success : function(result) {
  });

            $("#test_ID").empty(); //emtpy the UL before starting
            $.each(arr_json, function(i, v, d) {
             $("#test_ID").append("<li id='" + v.id +'" + v.test +"' >" + v.testDate + "<li>");
        });
    }
   });
 });

HTML

 <li id="test_ID"></li>

I do receive a couple of errors. The Line:

$("#test_ID").append("<li id='" + v.id +'" + v.test +"' >" + v.testDate + "<li>");

gives the following error: invalid number of arguments and unclosed String literal. I am also unsure of how to identify to specific elements in the JSON file.

Update I would like if the list element was in this format "Java 2016-08-01" and "JavaScript 2016-08-01". Thank you!

2
  • 1
    1. $.each( isn't closed. You need a closing curly brace and paren. 2. you haven't declared arr_json. It should be data Commented Feb 28, 2017 at 18:25
  • Changed "<li id='" + v.id +'" + v.test +"' >" + v.testDate + "<li>" to "<li id="' + v.id +"' + v.test + '" >' + v.testDate + '<li>' and try again. You had mismatched quotes. Commented Feb 28, 2017 at 18:25

2 Answers 2

2

You have a couple of errors in your javascript. See the corrected version below:

 $(function(){
   $.ajax({
     type : 'GET',
     url : 'json/data.json',
     async : false,
     beforeSend : function(){/*loading*/},
     dataType : 'json',
     success : function(result) {
            $("#test_ID").empty(); //emtpy the UL before starting

            // **************** correction made to the line below ***********************
            $.each(result, function(i, v, d) {                        

            // **************** correction made to the line below ***********************
            $("#test_ID").append('<li id="' + v.id + '">' + v.test + '  ' + v.testDate + '</li>');                         // correction made here
        });
    }
   });
 });
Sign up to request clarification or add additional context in comments.

2 Comments

This works great! But how fo I do if I want the format of the list element to look like this: "Java 2016-08-01" and "JavaScript 2016-08-012?
Thank you so much artemisian for taking a part of your valuable time to help me!
2

I did a quick fiddle. I don't quite understand what you're doing with v.test being unmarked inside the HTML tags so I did not include it. I used minimal JQuery to avoid complexity.

https://jsfiddle.net/ndx5da97/

  for (record in data) {
    $("#test_ID").append('<li id="' + data[record].id + '">' + data[record].testDate + '</li>');
  }

Hope this helps!

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.