lundi 25 avril 2016

Uncheck parent if any child is unchecked

I have an online store with WooCommerce, and I'm trying to users correctly choose the categories.

The idea is that if they select a category child, the parent category is selected too.

This I have done, but when uncheck the category child (and there is no input checked) the parent remains checked.

$('ul.product_cat_list').find('input[type=checkbox]').each(function(index, input) {
  $(input).bind('change', function() {
    var checkbox = $(this);
    var is_checked = $(checkbox).is(':checked');
    if(is_checked) {
        $(checkbox).parents('li').children('input').attr('checked', 'checked');
    } else {
      $(checkbox).parentsUntil('ul').find('input').removeAttr('checked');
    }
  });
});

Here the Fiddle




Aucun commentaire:

Enregistrer un commentaire