stevejansen
HTML
<h1>As a table</h1>
<table id="production_table" border="1">
<thead>
<tr>
<td>id</td>
<td>name</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Hello, World</td>
</tr>
<tr>
<td>2</td>
<td>Foo Bar</td>
</tr>
<tr>
<td>3</td>
<td>Fizz Buzz</td>
</tr>
</tbody>
</table>
<h1>As JSON</h1>
<code id="output"></code>
JavaScript
var records = (function () {
var table = document.getElementById('production_table'),
records = [];
if (table == null) return;
table = table.getElementsByTagName('tbody');
if (table == null || table.length === 0) return;
// there should only be one tbody element in a table
table = table[0];
// getElementsByTagName returns a NodeList instead of an Array
// but we can still use Array#forEach on it
Array.prototype.forEach.call(table.getElementsByTagName('tr'),
function (row) {
var record = [];
Array.prototype.forEach.call(row.getElementsByTagName('td'),
function (cell) {
record.push(cell.innerText);
});
records.push(record);
});
return records;
})();
document.getElementById('output').innerText = JSON.stringify(records);