iCheck and manual setting of checkbox to checked
Solution 1
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')
Solution 2
jQuery iCheck works bit different from the way you expect.
When you initialize a checkbox or radio button, it picks up the status/value of the control and builds the look and feel of that state. That is all. Then it never checks your control value or any update to its attributes.
So the only way to uncheck your checkbox in the Ajax failure event, is by using the functions exposed by the plugin.
You should use following in your ajax fail event to change the state back to previous
$(this).iCheck('toggle');
or you can change the html attributes and refresh the control like below
$(this).removeAttr('checked');
currentChecked.prop('checked', true);
$(this).iCheck('update'); — apply input changes, which were done outside the plugin
Solution 3
Below solution worked for me in iCheck v1.0.2. You can check iCheck radio by filtering radio button group like below
Using 'id' of element
$('input:radio[name="group"]').filter('[id="id1"]').iCheck('check');
Using 'value'
$('input:radio[name="group"]').filter('[value="id1"]').iCheck('check');
you can replace 'id' or 'value' dynamically base on your requirement/functionality. and you can change 'check' or 'uncheck' inside .iCheck('check').
mat_boy
Updated on March 12, 2020Comments
-
mat_boy over 4 years
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?
-
Igor F. over 4 yearsWhile this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.