iCheck and manual setting of checkbox to checked

mat_boy picture mat_boy · Apr 29, 2016 · Viewed 22.3k times · Source

I'm using the iCheck framework and I have two inputs

<input name="group" id="id1" type="radio" checked>
<input name="group" id="id2" type="radio">
<input name="group" id="id3" type="radio">
<input name="group" id="id4" type="radio">

On click I call an ajax function. If something fail, I want to set back the checked attribute to the previously selected input.

var currentChecked = $("input[name='group']:radio:checked");

$("input[name='group']:radio").each(function() {
        $(this).on('ifChecked', function(){
               $.ajax({
                    url: "/ajax/something/"
                })
                .done(function (data) {
                    currentChecked = $(this);
                })
                .fail(function (data) {
                    $(this).removeAttr('checked');
                    currentChecked.prop('checked', true);
                });
 });
});

But this will not reset the checked checkbox. There is something I don't see from the iCheck framework? Any solution?

Answer

Binayak Das picture Binayak Das · Sep 23, 2017

Actually in case of iCheck You need to update the iCheck to reflect the changes on Jquery.

You can update using iCheck('update')

$(this).iCheck('update');

After checked or unchecked the radio button using jquery.

Another Solution just check or uncheck the iCheck using his predefined function.

<input name="group" id="id1" type="radio" checked>

If the above one is your radio button you can use the code in jquery section like below

$('#id1').iCheck('uncheck'); //To uncheck the radio button
$('#id1').iCheck('check'); //To check the radio button

In this case no need to use the iCheck('update')