I need to put the table html data in a array . The current scenario does not allow me to add class
or id
to the <td>
tags (which would make it much easier), later I started the jquery code but I do not know how to proceed.
HTML
<table id="tb1">
<thead>
<tr>
<th>Bola1</th>
<th>Bola2</th>
<th>Bola3</th>
</tr>
</thead>
<tbody>
<tr><td>3</td><td>10</td><td>5</td></tr>
<tr><td>1</td><td>4</td><td>3</td></tr>
<tr><td>3</td><td>2</td><td>6</td></tr>
</tbody>
</table>
JQUERY
It would be something like this:
$(document).ready(function () {
$('.btnok').on('click', function () {
var dados = [];
var table = $("#tb1 tbody");
table.find("tr").each(function (indice) {
$(this).find('td').each(function (indice) {
dados[indice] = new Object();
dados[indice][indice] = $(this).text();
});
});
});
});
I expect to return a JSON:
[
{"Bola1":"3","Bola2":"10","Bola3":"5"},
{"Bola1":"1","Bola2":"4","Bola3":"3"},
{"Bola1":"3","Bola2":"2","Bola3":"6"}
]