I’ve got a HTML table and for each row I’ve got a checkbox, some cells with text and a cell with a input field, I need to convert all the rows except the one selcted into a javascript array so I can then pass the array to ajax and process with PHP.
This is what i've tried so far
var TableData = new Array();
$('.kmg_admin_millesimal_buildings_table tr input[type=checkbox]:not(:checked)').each(function(row, tr) {
TableData[row] = {
"Codice": $(tr).find('td:eq(1)').text(),
"Piano": $(tr).find('td:eq(2)').text(),
"Interno": $(tr).find('td:eq(3)').text(),
"Millesimi": $(tr).find('td').eq(4).find('input').val()
}
});
TableData.shift();
TableData.pop();
console.log(TableData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="kmg_admin_millesimal_buildings_table">
<thead>
<tr>
<th class="text-center"> Escludi</th>
<th class="text-center"> Codice</th>
<th class="text-center"> Piano</th>
<th class="text-center"> Interno</th>
<th> Millesimi</th>
</tr>
</thead>
<tbody class="kmg_admin_millesimal_buildings_table_body">
<tr>
<td class="text-center"><input type="checkbox" class="pippo"></td>
<td class="text-center">4E</td>
<td class="text-center">1</td>
<td class="text-center">4E</td>
<td><input type="text" class="km-millesimi" maxlength="6"></td>
</tr>
<tr>
<td class="text-center"><input type="checkbox" class="pippo"></td>
<td class="text-center">9N</td>
<td class="text-center">2</td>
<td class="text-center">9N</td>
<td><input type="text" class="km-millesimi" maxlength="6"></td>
</tr>
<tr>
<td class="text-center"><input type="checkbox" class="pippo"></td>
<td class="text-center">2C</td>
<td class="text-center">3</td>
<td class="text-center">2C</td>
<td><input type="text" class="km-millesimi" maxlength="6"></td>
</tr>
</tbody>
<tfoot class="custom-table-footer">
<tr>
<td colspan="4" class="text-right"></td>
<td class="km_total_millesimal_table font-green-sharp">0.00</td>
</tr>
</tfoot>
<table>
The error is that the array is not created
pop()it you have one row. then youshift()it and now you have an empty array. that's probably the problem.