I am trying to select all items in my multiselect form.
My form is working fine but if I unselect any item after making select all it never selected again even by click Select all. But individually I can select it.
below is my code:
$('.selectall').click(function() {
if ($(this).is(':checked')) {
$('.select-all').attr('checked', true);
$('.select-all').closest(".bold").find('input').attr('checked', true);
} else {
$('.select-all').attr('checked', false);
$('.select-all').closest(".bold").find('input').attr('checked', false);
}
});
$('.select-all').click(function() {
if ($(this).is(':checked')) {
$(this).closest(".bold").find('input').attr('checked', true);
} else {
$(this).closest(".bold").find('input').attr('checked', false);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="needs-validation" novalidate>
<ul class="Items-Select">
<li class="search-items">
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-search text-info" style="font-size:24px"></i>
</span>
<input type="search" class="form-control" placeholder="Search" onkeyup="mySearch()" id="Search">
<span class="input-group-btn">
<button class="btn btn-default" type="reset">
<i class="fa fa-times-circle-o text-danger" style="font-size:24px"></i>
</button>
</span>
</div>
</li>
<li class="bold">
<label class="checkbox">
<input type="checkbox" class="selectall"> Select all
</label>
</li>
<li class="bold">
<label class="checkbox">
<input type="checkbox" class="select-all"> Group 1
</label>
<span class="caret-container"><b class="caret"></b></span>
<ul class="group" id="groupa">
<li class="dropdown-item">
<label class="checkbox">
<input type="checkbox" value="1-1"> Option 1.1
</label>
</li>
<li class="dropdown-item">
<label class="checkbox">
<input type="checkbox" value="1-2"> Option 1.2
</label>
</li>
<li class="dropdown-item">
<label class="checkbox">
<input type="checkbox" value="1-3"> Option 1.3
</label>
</li>
</ul>
</li>
<li class="bold">
<label class="checkbox">
<input type="checkbox" class="select-all"> Group 2
</label>
<span class="caret-container"><b class="caret"></b></span>
<ul class="group" id="groupb">
<li class="dropdown-item">
<label class="checkbox">
<input type="checkbox" value="2-1"> Option 2.1
</label>
</li>
<li class="dropdown-item">
<label class="checkbox">
<input type="checkbox" value="2-2"> Option 2.2
</label>
</li>
<li class="dropdown-item">
<label class="checkbox">
<input type="checkbox" value="2-3"> Option 2.3
</label>
</li>
</ul>
</li>
</ul>
</form>
This is working currently for once when directly I use Select All, but unfortunately When I perform unselect on any that specifically will not selected with Select All or group Selection.