I am trying to push the checked row of HTML table only to array using jQuery.I need to have array index as the table header and value as the shell value of the checked column shell of the respective head.
I have HTML table as:
<table class="table table-bordered" id="checkTable">
<thead>
<th style="width:20px;"><input type="checkbox" name="headercheck" id="checkAll"></th>
<th>Programming Languages</th>
<th>Popularity</th>
</thead>
<tbody>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Java</td>
<td>62%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Python</td>
<td>46%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>JavaScript</td>
<td>38%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>C++</td>
<td>31%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>C#</td>
<td>27%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>PHP</td>
<td>14%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Pearl</td>
<td>14%</td>
</tr>
</tbody>
</table>
<button id='pushToArray'>Push To Array</button>
Now I need to push the selected rows of the table with the index value as the table head of the respective column.
$('div#tableContainer').find('button#pushToArray').on('click', function(){
var array = new Array();
var headers = new Array();
table.find('th').each(function(index, item) {
if(index == 0)
{
return true;
}
else
{
headers[index] = $(item).html();
}
});
var valueList = [];
table.find('tbody tr').each(function() {
$(this).find('input.table-checkbox:checked').each(function(){
var values = [];
$(this).closest('td').siblings('td').each(function(index, item){
values[headers[index]] = $(item).html();
});
valueList.push(values);
});
});
console.log(valueList);
});
I build headers successfully but while indexing i am getting my array as
[…]
0: []
"Programming Languages": "14%"
length: 0
undefined: "PHP"
__proto__: Array []
length: 1
__proto__: Array []
what can be the best way for getting things right in the way i wanted it to be.
$('div#tableContainer').find('button#pushToArray').on('click', function() {
var array = new Array();
var headers = new Array();
table.find('th').each(function(index, item) {
if (index == 0) {
return true;
} else {
headers[index] = $(item).html();
}
});
var valueList = [];
table.find('tbody tr').each(function() {
$(this).find('input.table-checkbox:checked').each(function() {
var values = [];
$(this).closest('td').siblings('td').each(function(index, item) {
values[headers[index]] = $(item).html();
});
valueList.push(values);
});
});
console.log(valueList);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered" id="checkTable">
<thead>
<th style="width:20px;"><input type="checkbox" name="headercheck" id="checkAll"></th>
<th>Programming Languages</th>
<th>Popularity</th>
</thead>
<tbody>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Java</td>
<td>62%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Python</td>
<td>46%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>JavaScript</td>
<td>38%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>C++</td>
<td>31%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>C#</td>
<td>27%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>PHP</td>
<td>14%</td>
</tr>
<tr>
<td><input type="checkbox" class="table-checkbox"></td>
<td>Pearl</td>
<td>14%</td>
</tr>
</tbody>
</table>
<button id='pushToArray'>Push To Array</button>
'div#tableContainer',tablevariable?? please post minimal reproducible example