drgullin / icheck

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

Updating the hidden checkbox with 'checked' #153

Closed texelate closed 10 years ago

texelate commented 10 years ago

Fantastic plugin!

One thing though, so that existing JavaScript validation works shouldn't you add the 'checked' attribute to the hidden input elements?

Currently I'm doing it thus:

$('input').iCheck({

    checkboxClass: 'icheckbox_minimal-blue',
    radioClass:    'iradio_minimal-blue'

}).on('ifChecked', function() {

    $(this).attr('checked', 'checked')
           .trigger('change');

}).on('ifUnchecked', function() {

    $(this).removeAttr('checked')
           .trigger('change');

});

iCheck doesn't seem to update the element at all. One thing I can't for the life of me figure out is how iCheck sets the GET/POST data to be sent. All that seems to update is the 'check' class gets added.

drgullin commented 10 years ago

Thanks @texelate!

You should use a 2.x version - https://github.com/fronteed/iCheck/tree/2.x-beta, since a previous version doesn't handle the change event.

Don't worry, checked state is changed if visual state is updated. In most of the browsers you won't see any updates in the HTML, just because developer tools don't show these changes.

drgullin commented 10 years ago

Actually, you don't need so much code from your example.

You can simply append this one before you include the iCheck:

window.icheck = {
  checkboxClass: 'icheckbox_minimal-blue',
  radioClass:  'iradio_minimal-blue'
};

Then, just add icheck class to the inputs or any HTML elements with inputs inside for the checkboxes and radios customization. iCheck applies changes itself, you don't have to add any code like $(this).removeAttr('checked').

texelate commented 10 years ago

Thanks @fronteed! I think I was getting confused because of the lack of the change event. I will try 2.x, thanks.

And thanks again for a great plugin!