Advertisement
Stann

Untitled

Nov 22nd, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Write a script using jQuery that reads a JSON string which contains information about cars and generates
  2.  an HTML table.*/
  3.  
  4. $(document).ready(function () {
  5.  
  6.     $.getJSON('input.json', function(data){
  7.  
  8.         $('body').append('<table></table>');
  9.         $('table').append('<tr id="header"></tr>');
  10.  
  11.         // appending the table headers
  12.         $.each(data[0], function (key) {
  13.             $('#header').append('<td>' + key + '</td>');
  14.         });
  15.  
  16.         // appending the data
  17.         for (var i = 0; i < data.length; i++) {
  18.             var row = '<tr id="' + i + '"></tr>';
  19.             $('table').append(row);
  20.  
  21.             $.each(data[i], function (_, value) {
  22.                 $('#' + i).append('<td>' + value + '</td>')
  23.             });
  24.         }
  25.     });
  26. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement