I have done some searching and used my cat as a rubber duck but I am really stuck with creating a function that takes an object and table id as arguments to spit out a simple table.
I am using basic javascript, as I am teaching myself it by diving in. You can view the whole code at http://oxygen.meddleso.me/ but I will post what I believe to be the most relevant here:
//html
<div id="inventoryTableDiv">
<table>
<tbody id='invtbody'></tbody>
</table>
</div>
//main.js
var inventory = [{ice: 10}, {metal: 10}];
function refreshValues() {
/* lots of other lines snipped */
generateTable(inventory, invtbody);
}
function generateTable(array, tablebodyid) {
var tablebody = document.getElementById(tablebodyid);
var tr = "<tr>";
for (var i = 0; i < array.length; i++) {
obj = array[i]
for (var x in obj) {
console.log(x + " : " + obj[x]);
tr += "<td>" + x + "</td>" + "<td>" + obj[x] + "</td>" + "</tr>";
console.log(tr);
tablebody.innerHTML = tr;
}
}
}
generateTable is based on this answer: Create HTML table from JavaScript object
It spits out the first log, "ice : 10" and the first set of tr:
"<tr><td>ice</td><td>10</td></tr>"
but I get the error:
Cannot set property 'innerHTML' of null
If I just console.log'd "tr" how can it be null? Any help would be appreciated and I hope this wasn't a terrible SO question.