I have nestable list with checkboxes and I need to work that check parent checkbox make all child checked and save children values into array.
I already figure out checking of all child checkboxes, when parent is checked? But I am not able to save their values at the same time.
HTML
<ul>
<li class="main-parent">
<input class="main-checkbox" type="checkbox" value="id">
<ul>
<li>
<input class="sub-checkbox" type="checkbox" value="1">
</li>
<li>
<input class="sub-checkbox" type="checkbox" value="2">
</li>
<li>
<input class="sub-checkbox" type="checkbox" value="3">
</li>
</ul>
</li>
</ul>
JQuery
$('input:checkbox').change(function () {
var array=[];
if(type === "main-checkbox") {
var parent = $(this).closest('.dd-item');
if('$(parent).find('.sub-checkbox').is(":checked")) {
array.push($(parent).find('.sub-checkbox').prop('checked',this.checked).val());
}
}
});
This one return empty array and nothing is saved. Is possible to do it somehow like that?