user1788736
user1788736

Reputation: 2845

How to populate html table using javascript without using div?

I am trying to fill html table(my table got 3 columns) with data from json using javascript but the data never get filled using div method! could any tell me how to fill content of table rows using without using div ?

 for(i in json)
    {


    var div = "<tr id=\""+i+"\">\n" +
    "<td>"+i+"</td>\n" +
    "<td><img src=\""+ json[i].thumb +"\" height=\"42\" width=\"42\"></td>\n" +
    "<td>\n" +
    "<a href=\"javascript:callfunction('Name=" + json[i].title + "','" + json[i].sources + "','"+ json[i].thumb +"')\" onclick=\"selectLink(this);\">" + json[i].title + "</a><br> \n" +
    "<br></td></tr>\n\n";

    $("#myDiv").append(div);

        }

Table to be filled:

   <table id="list" cellspacing="0" border="1">

    <tr>
    <th>Item#</th>
    <th>Logo</th>
    <th>Description</th>

    </tr>


    <div id='myDiv'></div>


    </table>

Upvotes: 0

Views: 1436

Answers (1)

num8er
num8er

Reputation: 19372

This Your new table:

<table id="list" cellspacing="0" border="1">
  <thead>
    <tr>
      <th>Item#</th>
      <th>Logo</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>

  </tbody>
</table>

And code:

var html = '';

for(var i in json)
{
    html += '<tr id="'+i+'">'; 
    html += '<td>'+i+'</td>'; 
    html += '<td><img src="'+json[i].thumb+'" height="42" width="42"></td>';
    html += "<td>";
    html += "<a href=\"javascript:callfunction('Name=" + json[i].title + "','" + json[i].sources + "','"+ json[i].thumb +"')\" onclick=\"selectLink(this);\">" + json[i].title + "</a><br/><br/>"; 
    html += '</td>';
    html += '</tr>';
}

$('#list > tbody').html(html);

Upvotes: 3

Related Questions