Several things:
- The variable
row
of for
does not exist, your variable condition loop is i
- In this loop the stop condition must be
tab.rows.length
-
row.cells
gives you failure because row
is undefined
- Regarding the problem that you get all the rows. The best thing is that, on the one hand, remove the button from your table and on the other, for the header type the
tag <th>
instead of <td>
and in the loop put a condition with indexOf()
so that you do not get those rows with tag <th>
You should have something like this:
let tab=document.getElementById('ordenT');
let jsonArr=[];
for(let i=0; i<tab.rows.length; i++){
if(tab.rows[i].innerHTML.indexOf("<th")<0){
let col=tab.rows[i].cells;
let jsonObj= { folio: col[0].innerHTML }
jsonArr.push(jsonObj);
}
}
console.log(jsonArr);
//CODIGO HTML
<table class="form_table" id="ordenT" width="100%">
<thead>
<th style="text-align: center; background-color: #fff0b3 !important">
<b>Órdenes de Trabajo.</b>
</th>
</tr>
<tr>
<td>
Dato 1
</td>
</tr>
<tr>
<td>
Dato 2
</td>
</tr>
</table>
<input type="button" class="button" id="txtIdTramite" name="txtIdTramite" value="ASIGNAR ORDENES DE TRABAJO" autofocus="autofocus" onclick="enviar();"></input>
PS: You will notice that instead of var
I am using let
. If your browser does not support let
(Internet Explorer < 11) use var
. Here you have more info.
Edit (with the button inside the table)
let tab=document.getElementById('ordenT');
let jsonArr=[];
for(let i=0; i<tab.rows.length; i++){
if(tab.rows[i].innerHTML.indexOf("<th")<0 && tab.rows[i].innerHTML.indexOf("button") < 0){
let col=tab.rows[i].cells;
let jsonObj= { folio: col[0].innerHTML }
jsonArr.push(jsonObj);
}
}
console.log(jsonArr);
<table class="form_table" id="ordenT" width="100%">
<thead>
<th style="text-align: center; background-color: #fff0b3 !important">
<b>Órdenes de Trabajo.</b>
</th>
</tr>
<tr>
<td>
Dato 1
</td>
</tr>
<tr>
<td>
Dato 2
</td>
</tr>
<tr>
<td style="text-align: center">
<input type="button" class="button" id="txtIdTramite" name="txtIdTramite" value="ASIGNAR ORDENES DE TRABAJO" autofocus="autofocus" onclick="enviar();"></input>
</td>
</tr>
</table>