Convert the HTML list from json files

advertisements

I have a problem with my code when listing countries from json file:

My HTML

<div id="country">country</div>

My JS Code

$.getJSON( "country.js", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    items.push( "<li id='" + key + "'>" + val + "</li>" );
  });

  $( "<ul/>", {
    html: items.join( "" )
  }).appendTo( "#country" );
});

Fiddle link


Your json source is wrong.

  1. remove var countryList =
  2. Find code: 'TF' and change it to "code": "TF"
  3. You accessed wrong variable.

    items.push( "<li id='" + val.countryCode + "'>" + val.countrName + "</li>" );