Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function "select all" and iCheck

I want to implement a "Select all" using iCheck.

This is what I've done so far:

$(function () {
    $('input').iCheck();
    $('input.all').on('ifChecked ifUnchecked', function(event) {
        if (event.type == 'ifChecked') {
            $('input.check').iCheck('check');
        } else {
            $('input.check').iCheck('uncheck');
        }
    });
    $('input.check').on('ifUnchecked', function(event) {
        $('input.all').iCheck('uncheck');
    });
});

Fiddle: jsfiddle.net/N7uYR/1/

I want that when "Check Box 1", "Check Box 2", "Check Box 3", "Check Box 4" are selected, "Select all" also gets selected.

Exactly like this: jsfiddle.net/ivanionut/N7uYR/

How can I do this?

like image 285
Ivan Avatar asked Jul 23 '13 20:07

Ivan


People also ask

How do I select all checkboxes with one checkbox?

In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.

How do you check if the iCheck is checked or not?

The Best Answer is const value = $('SELECTOR'). iCheck('update')[0]. checked; This directly returns true or false as boolean .

How do you check all checkbox is checked or not in Javascript?

type == 'checkbox' && inputs[x]. name == 'us') { is_checked = inputs[x]. checked; if(is_checked) break; } } // is_checked will be boolean 'true' if any are checked at this point.

How do you check all checkbox is checked or not in jQuery?

To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);


2 Answers

Here's what I came up with.

$(function () {
    var checkAll = $('input.all');
    var checkboxes = $('input.check');

    $('input').iCheck();

    checkAll.on('ifChecked ifUnchecked', function(event) {        
        if (event.type == 'ifChecked') {
            checkboxes.iCheck('check');
        } else {
            checkboxes.iCheck('uncheck');
        }
    });

    checkboxes.on('ifChanged', function(event){
        if(checkboxes.filter(':checked').length == checkboxes.length) {
            checkAll.prop('checked', 'checked');
        } else {
            checkAll.removeProp('checked');
        }
        checkAll.iCheck('update');
    });
});

jsFiddle

like image 153
Dzulqarnain Nasir Avatar answered Oct 14 '22 06:10

Dzulqarnain Nasir


For anyone still struggling with this, I've altered the accepted answer https://stackoverflow.com/a/17821003/3061836) from Dzulqarnain Nasir a bit.

I've changed the removeProp method to the prop method:

checkAll.removeProp('checked');

Is altered to

checkAll.prop('checked', false);

The removePropmethod didn't work for me, but setting the checked property to false worked.

At the time of writing, I'm using iCheck 1.0.2 with jQuery 2.1.4

like image 36
yerlix Avatar answered Oct 14 '22 07:10

yerlix