drgullin / icheck

Highly customizable checkboxes and radio buttons (jQuery & Zepto)
http://fronteed.com/iCheck
7.38k stars 1.62k forks source link

no update of iCheck after prevent a click event #225

Closed cantado closed 9 years ago

cantado commented 9 years ago

I want to show an error frame after checking more than 4 elements. The 5th elements input is not checked in my code, but iCheck add the 'checked' class to the element.

$("#container input").on('click', function(e){
  return $("#container input:checked").length <= 4;
}
drgullin commented 9 years ago

Please provide an example on jsfiddle.

cantado commented 9 years ago

the first example was wrong.. sry..

here is the jsfiddle code: http://jsfiddle.net/ooLcbmts/2/

the checkboxes are visible to see the status of the checkboxes. the js code on line 8 is to uncheck the checkbox if two others are checked. Without this line the callback does nothing.

How can i prevent checking the other checkboxes if two checkboxes are checked?

cantado commented 9 years ago

i added some ClassChange-trigger function in icheck.js v1.0.2

in the on function

parent[_add](specific || option(input, state) || ''); // <-- line 418
input.trigger('iCheckClassChange');
input.trigger('iCheckClassAdd');

in the off function:

parent[_add](regular || option(input, callback) || ''); // <-- line 460
input.trigger('iCheckClassChange');
input.trigger('iCheckClassRemove', { removedClass: (regular || option(input, callback) || '') });

then i listen to the event and remove the checked class if the checkbox is not checked:

$("input").on('iCheckClassAdd', function(e){
  if(!$(e.currentTarget).prop('checked')){
    $(e.currentTarget).parent().removeClass('checked');
  }
});
drgullin commented 9 years ago

Try to use 2.x version http://jsfiddle.net/ooLcbmts/4/

cantado commented 9 years ago

prevent the check does not work. i try to prevent the event if two checkboxes are checked, but it doesnt work.

i added a button to see the inputs value. http://jsfiddle.net/ooLcbmts/9/

the checkboxes aren't checked, but the icheck plugin added the 'checked' class and it seems like, the checkbox is checked.

the icheck plugin should prevent the event like without the plugin: http://jsfiddle.net/ooLcbmts/8/

do you have a workaround for preventing the click event?

drgullin commented 9 years ago

iCheck callbacks don't support return false or preventDefault. It just launches them as a functions and doesn't care about the result.

You can set disabled state instead. http://jsfiddle.net/ooLcbmts/10/, http://jsfiddle.net/ooLcbmts/11/

Note that v2 methods are a bit different https://github.com/fronteed/iCheck/tree/2.x#methods