I have a set of checkboxes generated using a for loop. I want the first checkbox to act as a parent, and all other checkboxes should act as children. When the parent checkbox is clicked, all child checkboxes should be checked.
Although I have code to achieve this functionality, it does not work as expected.
$('.checkbox-all-index-1').click(function () {
if($(this).is(':checked')) {
$('.checkbox-index').prop('checked', true);
} else {
$('.checkbox-index').prop('checked', false);
}
});
Let's assume the HTML structure is as follows:
<input class="group-selector-subscriber checkbox-index" type="checkbox">
<input class="group-selector-subscriber checkbox-index" type="checkbox">
<input class="group-selector-subscriber checkbox-index" type="checkbox">
<input class="group-selector-subscriber checkbox-index" type="checkbox">
Now, I need to assign the checkbox-all-index-1
class to the first checkbox created in the loop.
All these checkboxes are dynamically created through a loop, so assigning the class to one affects all others. I only want it to apply to the first one.
Thank you!