Reputation: 85
My question is how to get table cell element and append it form.
function submitTable(url) {
form = document.createElement('form');
form.method = 'POST';
form.action = url;
/*
HOW TO FIND ALL SELECTED CHECKBOX HERE
*/
document.body.appendChild(form);
form.submit();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="mytable" class="table">
<thead>
<tr>
<th scope="col">COL1</th>
<th scope="col">COL2</th>
</tr>
</thead>
<tbody>
<tr>
<td id="1">
<div>
<input class="form-check-input" type="checkbox" name="checkbox1[]" value="something">
</div>
<div>
<input class="form-check-input" type="checkbox" name="checkbox2[]" value="something">
</div>
</td>
<td id="2">
<div>
<input class="form-check-input" type="checkbox" name="checkbox1[]" value="something">
</div>
<div>
<input class="form-check-input" type="checkbox" name="checkbox2[]" value="something">
</div>
</td>
</tr>
<tr>
<td id="3">
<div>
<input class="form-check-input" type="checkbox" name="checkbox1[]" value="something">
</div>
<div>
<input class="form-check-input" type="checkbox" name="checkbox2[]" value="something">
</div>
</td>
<td id="4">
<div>
<input class="form-check-input" type="checkbox" name="checkbox1[]" value="something">
</div>
<div>
<input class="form-check-input" type="checkbox" name="checkbox2[]" value="something">
</div>
</td>
</tr>
</tbody>
</table>
<button id="MYFORM" class="btn btn-primary">Submit</button>
how to find specific cell on click and on cell click check both the checkbox, and when click the button, read all cells and get checked checkbox value as array and submit as form
I want when I click on cell not individual checkbox, when clicked on cell check both checkbox using jquery. Then, after selecting cells has been done, upon button id="MYFORM"
click I want to find all the checked checkboxes and submit them as a generated form.
Upvotes: 2
Views: 472
Reputation: 6682
To append all checked checkboxes, just use specify the appropriate collection as query selector:
function submitTable(url)
{
let form = document.createElement('form');
form.setAttribute('action', url);
$("#my-table>tbody input[type=checkbox]:checked").clone().appendTo(form);
document.body.appendChild(form);
form.submit();
}
Note that your current naming can not be distinguished. Generate names like name="checkbox1[1]"
specifying an explicit index.
You can select all checkboxes of a table row by
$('#my-table>tbody>tr').click(function(ev)
{
if(ev.target.tagName !== 'INPUT')
$(this).find('>td input[type=checkbox]').prop('checked', true);
})
Upvotes: 1