Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iCheck and manual setting of checkbox to checked

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?

like image 644
mat_boy Avatar asked Apr 29 '16 15:04

mat_boy


People also ask

How do you check radio button is checked or not?

Using Input Radio checked property: The Input Radio checked property is used to return the checked status of an Input Radio Button. Use document. getElementById('id'). checked method to check whether the element with selected id is check or not.

How do I check a checkbox?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.


1 Answers

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')

like image 65
Binayak Das Avatar answered Oct 27 '22 02:10

Binayak Das