javascript - Uncheck all checkboxes related to clicked button. -
the problem : have checkbox groups created dynamically. if @ least 1 checkbox checked title of group showing in page's top in separate div cross icon x. wanted on clicking each added title's cross x need uncheck checkboxes related title , remove title itself.
codepen example : https://codepen.io/gabe747/pen/jlaevq
$(document).ready(function() { check() }); $("input[type=checkbox]").change(function() { check() }) function check() { $(".col-filter_amount").text(""); $(".col-filter_block").each(function() { if ($(this).find("input[type=checkbox]:checked").length) $(this).find(".cols-title").clone().append('<a class="checkbox-remover" href="javascript:;">x</a>').appendto($(".col-filter_amount")) }) } $(".checkbox-remover").on('click', function() { // remove title , uncheck related title checkboxes }); .col-filter_amount { border: 1px solid #ccc; } .col-filter_amount h4 { background: #000; color: #fff; display: inline-block; padding: 5px; margin: 15px; } .col-filter_amount h4 { color: fff; text-decoration: none; margin-left: 5px; color: #000; background: #fff; border-radius: 50%; padding: 1px 5px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-filter_amount"> </div> <div class="col-filter_block"> <h4 class="cols-title" data-value="1">group1</h4> <input type="checkbox" data-value="1">item1 <input type="checkbox" data-value="1">item2 <input type="checkbox" data-value="1">item3 <input type="checkbox" data-value="1">item4 </div> <div class="col-filter_block"> <h4 class="cols-title" data-value="2">group2</h4> <input type="checkbox" data-value="2">item1 <input type="checkbox" data-value="2">item2 <input type="checkbox" data-value="2">item3 <input type="checkbox" data-value="2">item4 </div> thanks support!
because dynamically added element need listen click event on parent element exists, body example. can use data attribute select input elements same value.
$(document).ready(function() { check() }); $("input[type=checkbox]").change(function() { check() }) function check() { $(".col-filter_amount").text(""); $(".col-filter_block").each(function() { if ($(this).find("input[type=checkbox]:checked").length) $(this).find(".cols-title").clone().append('<a class="checkbox-remover" href="javascript:;">x</a>').appendto($(".col-filter_amount")) }) } $('body').on('click', ".checkbox-remover", function() { $('input[data-value="'+$(this).parent().data('value')+'"]').attr('checked', false) $(this).parent().remove() }); .col-filter_amount { border: 1px solid #ccc; } .col-filter_amount h4 { background: #000; color: #fff; display: inline-block; padding: 5px; margin: 15px; } .col-filter_amount h4 { color: fff; text-decoration: none; margin-left: 5px; color: #000; background: #fff; border-radius: 50%; padding: 1px 5px; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="col-filter_amount"> </div> <div class="col-filter_block"> <h4 class="cols-title" data-value="1">group1</h4> <input type="checkbox" data-value="1">item1 <input type="checkbox" data-value="1">item2 <input type="checkbox" data-value="1">item3 <input type="checkbox" data-value="1">item4 </div> <div class="col-filter_block"> <h4 class="cols-title" data-value="2">group2</h4> <input type="checkbox" data-value="2">item1 <input type="checkbox" data-value="2">item2 <input type="checkbox" data-value="2">item3 <input type="checkbox" data-value="2">item4 </div>
Comments
Post a Comment