drgullin / icheck

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

How to use iCheck with Live #37

Closed billmn closed 11 years ago

billmn commented 11 years ago

How can I apply iCheck to checkboxes/radios loaded from ajax? In a normal HTML page works fine, but if I load this page from ajax, iCheck isn't applied.

I'm using this code :

$('input[type=checkbox], input[type=radio]').iCheck({
    checkboxClass: 'icheckbox_square-blue',
    radioClass: 'iradio_square-blue'
});

Cheers

pocesar commented 11 years ago

create a delegated function that applies your iCheck, then call it later from the AJAX callback

$(document).on('icheck', function(){
  $('input[type=checkbox], input[type=radio]').iCheck({
    checkboxClass: 'icheckbox_square-blue',
    radioClass: 'iradio_square-blue'
  });
}).trigger('icheck'); // trigger it for page load

then in your AJAX

$.ajax(...).done(function(){
  // deal with your data
  $(document).trigger('icheck'); // apply icheck to checkboxes
});

Of course, you can just set a global function that does that, but cluttering the global space isn't a good idea

billmn commented 11 years ago

Works fine! thank you

rumends commented 11 years ago

There is a little problem whit this delegation. If you use $('#mycheckbox').on('ifToggled', function(){ // custom function }); to bind custom function to specific checkbox after ajaxs trigger 'icheck' there is no bind function to $('#mycheckbox') Is there any solution for this?