I am having 100s of items in my datatable, displayed using 'forEach' in script as below
<div class="panel-body">
<table class="table table-striped" id="table1">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Customer</th>
<th scope="col">CheckBoxes</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" varStatus="status" var="name">
<tr>
<td>${status.index + 1}</td>
<td>${name.getName()}</td>
<td>${name.getCustomer()}</td>
<td>
<input type="checkbox" class="custom-control-input" id="checkRule" value="${rule.getName()}">
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
What I am trying is, upon checking respective checkboxes, I need those value to be fetched and sent in AJAX (POST).
$(document).ready(function(){
const myArray = [];
$('#checkRule').click(function(){
console.log('checked');
if( $('#checkRule').is(':checked') ) {
myArray.push( $('#checkRule').val() );
console.log(myArray);
}else{
const index = myArray.indexOf( $('#checkRule').val() );
if (index > -1) {
myArray.splice(index, 1);
}
console.log(myArray);
}
});
$('#correlation_table').DataTable();
$('#send').click(function(){
var result = myArray;
console.dir('Import result >>>>> ',result);
$.ajax({
url: "sendAll",
headers: {
'Accept': 'text/plain',
'Content-Type': 'application/json'
},
type: "POST",
dataType: "html",
data: JSON.stringify(result),
success :function(data) {
console.log(data);
},
error :function(err){
console.log(err);
}
});
});
});
Upon using this above code, I am reaching nowhere. Console output for "console.log('Import result >>>>> ',result);" is
Import result >>>>> Array []
How can I get all 'Name' upon checking multiple checkboxes in table?