javascript - Checkbox Grouping in DIvs -
i want group checkbox in following 2 divs, when user checks on of checkbox, other checkboxes in div uncheck.
how can using jquery?
i trying use following jquery snippet not working.
$('input[type="checkbox"]').on('change', function() { $(this).siblings('input[type="checkbox"]').prop('checked', false); });
the following html groups of checkboxes.
<!--- group 1 --> <div id="banner_main" class="row"> <div class="question"> banner main </div> <div class="switch"> <input id="cmn-toggle-banner_main-admob" class="cmn-toggle cmn-toggle-yes-no" checked type="checkbox"> <label for="cmn-toggle-banner_main-admob" data-on="admob" data-off="admob"></label> </div> <div class="switch"> <input id="cmn-toggle-banner_main-fb" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-banner_main-fb" data-on="fb" data-off="fb"></label> </div> <div class="switch"> <input id="cmn-toggle-banner_main-mc" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-banner_main-mc" data-on="mc" data-off="mc"></label> </div> <div class="switch"> <input id="cmn-toggle-banner_main-appnext" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-banner_main-appnext" data-on="appnext" data-off="appnext"></label> </div> </div> <!--- group 2 --> <div id="banner_wp" class="row"> <div class="question"> banner wallpaper </div> <div class="switch"> <input id="cmn-toggle-banner_wp-admob" class="cmn-toggle cmn-toggle-yes-no" checked type="checkbox"> <label for="cmn-toggle-banner_wp-admob" data-on="admob" data-off="admob"></label> </div> <div class="switch"> <input id="cmn-toggle-banner_wp-fb" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-banner_wp-fb" data-on="fb" data-off="fb"></label> </div> <div class="switch"> <input id="cmn-toggle-banner_wp-mc" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-banner_wp-mc" data-on="mc" data-off="mc"></label> </div> <div class="switch"> <input id="cmn-toggle-banner_wp-appnext" class="cmn-toggle cmn-toggle-yes-no" type="checkbox"> <label for="cmn-toggle-banner_wp-appnext" data-on="appnext" data-off="appnext"></label> </div> </div>
checkboxes not siblings of each other,their parent divs are. need traverse checked checkbox parent, siblings , find checkboxes in it:
$('input[type="checkbox"]').on('change', function() { $(this).parent().siblings().find('input[type="checkbox"]').prop('checked', false); });
you can use:
$('input[type="checkbox"]').on('change', function() { $(this).closest('.row').find(':checkbox').not(this).prop('checked', false); });
Comments
Post a Comment