Adam
Adam

Reputation: 5

putting JSON into table rows

Well, why this code is not working? I copy it for jsfiddle , but it's not working.. the latest library is included, so I really don't know why it's not working.. ;/

Code:

<html>

    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script>
            var jsondata = $.parseJSON('{"response":[["name0","id0","amt0"],["name1","id1","amt1"]]}');
            $.each(jsondata.response, function (i, d) {
                var row = '<tr>';
                $.each(d, function (j, e) {
                    row += '<td>' + e + '</td>';
                });
                row += '</tr>';
                $('#table tbody').append(row);
            });
        </script>
    </head>

    <body>
        <div id="myDiv">
            <table id="table">
                <thead>
                    <tr>
                        <th>header1</th>
                        <th>header2</th>
                        <th>header3</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </body>

</html>

Upvotes: 0

Views: 96

Answers (1)

tymeJV
tymeJV

Reputation: 104775

You need to wrap your code in:

$(document).ready(function() {

    //code here
    var jsondata=$.parseJSON('{"response":[["name0","id0","amt0"],["name1","id1","amt1"]]}');

    $.each(jsondata.response, function(i, d) {
        var row='<tr>';
        $.each(d, function(j, e) {
             row+='<td>'+e+'</td>';
        });
        row+='</tr>';
        $('#table tbody').append(row);
    });

});

Your code works: http://jsfiddle.net/ayqcf/

Upvotes: 2

Related Questions