javascript - Remove class in jQuery -
i have issue when trying remove class in jquery.
checkboxes in following structure. when click on checkbox check adds class checkbox in span , shows checkbox checked.
i want add condition if checked checkboxes greater one not add checkbox class on next checkbox when click on or remove class based on clicked checkbox.
i using following jquery code not working.
$(document).ready(function() { $("label").on("click", function(e) { var abc = $('.custom.checkbox.checked').length; if (abc > 1) { $(this).children('.custom.checkbox.checked').removeclass('checked'); alert(abc); } });
<label for="field_422_10" class="foo"> <input type="checkbox" value="yoga-tai chi-pilates" id="field_422_10" name="field_389[]" style="display: none;"> <span class="custom checkbox"></span> yoga-tai chi-pilates </label> <label for="field_422_11" class="foo"> <input type="checkbox" value="yoga-tai chi-pilates" id="field_422_10" name="field_389[]" style="display: none;"> <span class="custom checkbox"></span> yoga-tai chi-pilates2 </label>
if right, want have max allowed selections checkboxes. can that:
$("label").on("click", function(e) { var max_count = 10; var input = $(this).find('input'); var span = $(this).find('span'); var checked_boxes = $('#myboxes').find('input:checked'); if (checked_boxes.length > max_count && input.is(':checked')) { // uncheck input input.prop('checked', false); // remove class span span.removeclass('checked'); } else { if (input.is(':checked')) span.removeclass('checked'); else span.addclass('checked'); } });
Comments
Post a Comment