60

I am using iCheck plugin for customizing checkboxes. I need to display certain text when one or more checkbox is checked and hide the text when none are checked.

The code I have currently displays the text on first click but doesn't hide it unless I click 2 more times. I have multiple checkboxes and would like to show text if one of 'em are checked else hide the text. Does anybody have any idea? The plugin has:

ifChecked
ifChanged
ifClicked
ifUnchecked
ifToggled
ifDisabled
ifEnabled.......

callbacks....Here is the plugin function

$('input').iCheck({ 
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue',
increaseArea: '20%' // optional
});

Here is what I tried..

$('input').on('ifChecked', function(event){
$(".hide").toggle();
});

html

<input type="checkbox">
<div class"hide" style="display:none">Hi there</div>
user3006683
  • 783
  • 2
  • 9
  • 16
  • 1
    This might give you some direction `:)` http://stackoverflow.com/questions/2660323/jquery-checkboxes-and-ischecked P.S. Post is bit convoluted thought I will paste the `is(':checked')` flag so that you can use it accordingly. flick a jsfiddle I should be able to help you. – Tats_innit Dec 23 '13 at 02:43

15 Answers15

64

For those who struggle with this:

const value = $('SELECTOR').iCheck('update')[0].checked;

This directly returns true or false as boolean.

Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64
34

you can get value of checkbox and status by

$('.i-checks').on('ifChanged', function(event) {
    alert('checked = ' + event.target.checked);
    alert('value = ' + event.target.value);
});
grud phunsanit
  • 441
  • 4
  • 4
21

Check this :

var checked = $(".myCheckbox").parent('[class*="icheckbox"]').hasClass("checked");

if(checked) {
  //do stuff
}
Mohamed Ramrami
  • 12,026
  • 4
  • 33
  • 49
  • 3
    This does not work well. I am collecting the `checked`class state of a lot of iCkeck-boxed, but the code that sets the `check` class is slower than my specific code that reads the class. Thus this method has timing issues and should be avoided. – PIDZB Jun 29 '17 at 12:53
15

All callbacks and functions are documented here: http://fronteed.com/iCheck/#usage

$('input').iCheck('check'); — change input's state to checked
$('input').iCheck('uncheck'); — remove checked state
$('input').iCheck('toggle'); — toggle checked state
$('input').iCheck('disable'); — change input's state to disabled
$('input').iCheck('enable'); — remove disabled state
$('input').iCheck('indeterminate'); — change input's state to indeterminate
$('input').iCheck('determinate'); — remove indeterminate state
$('input').iCheck('update'); — apply input changes, which were done outside the plugin
$('input').iCheck('destroy'); — remove all traces of iCheck
Evalds Urtans
  • 6,436
  • 1
  • 41
  • 31
13

You just need to use the callbacks, from the documentation: https://github.com/fronteed/iCheck#callbacks

$('input').on('ifChecked', function(event){
  alert(event.type + ' callback');
});
user1883000
  • 231
  • 2
  • 9
  • 30
    how the hell is this responding to the quetion? The question was clear how to determine if the checkbox is checked or not, not what `type` of event was raised!! – DATEx2 Oct 28 '15 at 16:17
10

To know if the iCheck box is checked

var isChecked = $("#myicheckboxid").prop("checked");
Ram Pratap
  • 1,079
  • 11
  • 8
7

I wrote some simple thing:

When you initialize icheck as:

$('input').iCheck({
    checkboxClass: 'icheckbox_square-blue',
    radioClass: 'iradio_square-blue',
    increaseArea: '20%' // optional
});

Add this code under it:

$('input').on('ifChecked', function (event){
    $(this).closest("input").attr('checked', true);          
});
$('input').on('ifUnchecked', function (event) {
    $(this).closest("input").attr('checked', false);
});

After this you can easily find your original checkbox's state. I wrote this code for using icheck in gridView and accessed its state from server side by C#.

Simply find your checkBox from its id.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
Shakti
  • 723
  • 8
  • 15
5

Use this code for iCheck:

$('.i-checks').iCheck({
    checkboxClass: 'icheckbox_square-green',
    radioClass: 'iradio_square-green',
}).on('ifChanged', function(e) {
    // Get the field name
    var isChecked = e.currentTarget.checked;

    if (isChecked == true) {

    }
});
Kyll
  • 7,036
  • 7
  • 41
  • 64
Abdus Salam Azad
  • 5,087
  • 46
  • 35
3

Call

element.iCheck('update');

To get the updated markup on the element

Shareef
  • 351
  • 4
  • 12
3

Use following code to check if iCheck is checked or not using single method.

$('Selector').on('ifChanged', function(event){

    //Check if checkbox is checked or not
    var checkboxChecked = $(this).is(':checked');

    if(checkboxChecked) {
        alert("checked");
    }else{
        alert("un-checked");
    }
});
Imran Ali
  • 372
  • 5
  • 16
2

If any one have any interest on multiple icheck(). Then you can go for it with the below one:

$("#mother_checkbox").on("ifChanged", function(){
        if($(this).is(':checked')) {
            $('.child_checkbox').prop('checked', true);
            $('.child_checkbox').iCheck('update');
        }
        else{
            $('.child_checkbox').prop('checked', false);
            $('.child_checkbox').iCheck('update');
        }
});

It will Check/Uncheck all the child checkbox. Good Day !

Tushar Roy
  • 1,018
  • 10
  • 18
1
$('input').on('ifChanged', function(event) {
             if($(".checkbox").is(":checked")) {
                $value = $(this).val();
             }

             else if($(".checkbox").is(":not(:checked)")) {
                $value= $(this).val();
             }
        });
User0706
  • 1,067
  • 1
  • 18
  • 34
1

Use this method:

$(document).on('ifChanged','SELECTOR', function(event){
  alert(event.type + ' callback');
});
maverabil
  • 188
  • 3
  • 16
0

this works for me... try it

// Check #x
$( "#x" ).prop( "checked", true );
 
// Uncheck #x
$( "#x" ).prop( "checked", false );
Monzur
  • 1,341
  • 14
  • 11
0

You could wrap all your checkboxes in a parent class and check the length of .checked..

if( $('.your-parent-class').find('.checked').length ){
  $(".hide").toggle();
}
shroy
  • 918
  • 2
  • 10
  • 24