I’m confused if there is anyway to detect the check
and uncheck
checkbox in table. All I want is when I check the checkbox item on table it will add on array list if it is already check it should not add in arrayList otherwise it will add through push
, in short every item stored in array list should be unique, if I uncheck the item that already added in array list it should be remove , how do I achieve that? it possible? I’m being using console right now.
I have duplicate value on index 0 and 2 the output should only 0 and 1
since it already exist
I tried using just to unique the value before it prints but it doesn’t work
var unique = selected_items.filter(function(itm, i, selected_items) {
return i == selected_items.indexOf(itm);
});
var selected_items = [];
$("#allcb").change(function() {
$('tbody tr td input[type="checkbox"]').prop(
"checked",
$(this).prop("checked")
);
});
$('.checkboxtype').click(function(event){
var grid = document.getElementById("Table1");
var addedCheckboxes = $(".whole-div-class .row");
var checkBoxes = $(`input[type=checkbox]`);
var str = '';
for (var i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked && checkBoxes[i].name !== "allcb") {
var row = checkBoxes[i].parentNode.parentNode;
var stck_id = checkBoxes[i].id;
var array_id = {
id: stck_id,
item : row.cells[1].innerHTML,
quantity : row.cells[2].innerHTML
}
selected_items.push(array_id);
}
}
console.log("selected Items");
console.log(selected_items);
});
$("#allcb").change(function() {
$('tbody tr td input[type="checkbox"]').prop(
"checked",
$(this).prop("checked")
);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1" id="Table1">
<thead>
<tr>
<th>
<input type="checkbox" id="allcb" name="allcb" />
</th>
<th>Number</th>
<td>Age</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" class="dt-checkboxes form-check-input checkboxtype" id="cb1" name="cb1" />
</td>
<td>200</td>
<td> 25</td>
</tr>
<tr>
<td>
<input type="checkbox" class="dt-checkboxes form-check-input checkboxtype" id="cb2" name="cb2" />
</td>
<td>300</td>
<td>30</td>
</tr>
<tr>
<td>
<input type="checkbox" class="dt-checkboxes form-check-input checkboxtype" id="cb3" name="cb3" />
</td>
<td>400</td>
<td>50</td>
</tr>
</tbody>
</table>
<br />
<ul class="list-group mb-3 whole-div-class"></ul>