3

How do I display the contents of an array in a HTML table while using jQuery?

Here's my script... This outputs the objects in the array on top of the table not in the table.

HTML

<table>
<thead>
<tr>
<th>ITEM ID</th>
<th>NUMBER OF BAGS</th>
<th>WEIGHT</th>
</tr>
</thead>
<tbody>
<div id="returnlist"></div>
</tbody>
</table>

jQuery

var tempList = new Array();
$('#add').click(function(){
var split = $('#onhanditem').val().split('_');
var itemid = split['0']; 
var kilo = $('#kilo').val();
var bagsReturned = $('#bagsReturned').val();
var totalbagkiloreturned = kilo+'_'+bagsReturned;
tempList[itemid] = totalbagkiloreturned;
list = '';
// i = ITEM ID | tempList = totalbagkiloreturned
for (var i in tempList){
var itemID = i;
var split = tempList[i].split('_');
var kilo = split['0'];
var numBags = split['1'];
list+='<tr><td>'+itemID+'</td><td>'+kilo+'</td><td>'+numBags+'</td></tr>';
}
$('#returnlist').html(list);
}); 
});
Brian Tompsett - 汤莱恩
5,92772 gold badges63 silver badges135 bronze badges
asked Sep 27, 2011 at 13:15

2 Answers 2

1

As far as I'm aware, the middle of a table isn't a valid location for a <div> tag, which is why it's not being displayed inside the table. Why not put your id on the <tbody> tag instead, and do away with the div altogether?

answered Sep 27, 2011 at 13:18
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I wasn't aware it's possible to add an id in the tbody tag. I assume I can also add a class right?
@JohnSmith you can add id, class, title and style attributes to very nearly anything in HTML, the exceptions are basically only in <head>
1

You can't have a div inside a table, it's simply not valid HTML.

Try the following HTML instead

<table>
<thead>
<tr>
<th>ITEM ID</th>
<th>NUMBER OF BAGS</th>
<th>WEIGHT</th>
</tr>
</thead>
<tbody id="returnlist">
</tbody>
</table>
answered Sep 27, 2011 at 13:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.