[jquery] How to uncheck checkbox using jQuery Uniform library

I have a problem with unchecking a checkbox. Have a look at my jsFiddle, where I am attempting:

   $("#check2").attr("checked", true);

I use uniform for styling the checkbox and it simply does not work to check/uncheck the checkbox.

Any ideas?

This question is related to jquery checkbox

The answer is


you need to call $.uniform.update() if you update element using javascript as mentioned in the documentation.


$("#chkBox").attr('checked', false); 

This worked for me, this will uncheck the check box. In the same way we can use

$("#chkBox").attr('checked', true); 

to check the checkbox.


checkbox that act like radio btn

$(".checkgroup").live('change',function() { var previous=this.checked; $(".checkgroup).attr("checked", false); $(this).attr("checked", previous); });


_x000D_
_x000D_
 $("#checkall").change(function () {_x000D_
            var checked = $(this).is(':checked');_x000D_
            if (checked) {_x000D_
                $(".custom-checkbox").each(function () {_x000D_
                    $(this).prop("checked", true).uniform();_x000D_
                });_x000D_
            } else {_x000D_
                $(".custom-checkbox").each(function () {_x000D_
                    $(this).prop("checked", false).uniform();_x000D_
                });_x000D_
            }_x000D_
        });_x000D_
_x000D_
        // Changing state of CheckAll custom-checkbox_x000D_
        $(".custom-checkbox").click(function () {_x000D_
            if ($(".custom-checkbox").length == $(".custom-checkbox:checked").length) {_x000D_
                $("#chk-all").prop("checked", true).uniform();_x000D_
            } else {_x000D_
                $("#chk-all").removeAttr("checked").uniform();_x000D_
            }_x000D_
        });
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<input type="checkbox" id='checkall' /> Select All<br/>_x000D_
 <input type="checkbox" class='custom-checkbox' name="languages" value="PHP"> PHP<br/>_x000D_
 <input type="checkbox" class='custom-checkbox' name="languages" value="AngularJS"> AngularJS<br/>_x000D_
 <input type="checkbox" class='custom-checkbox' name="languages" value="Python"> Python<br/>_x000D_
 <input type="checkbox" class='custom-checkbox' name="languages" value="Java"> Java<br/>
_x000D_
_x000D_
_x000D_


In some case you can use this:

$('.myInput').get(0).checked = true

For toggle you can use if else with function


A simpler solution is to do this rather than using uniform:

$('#check1').prop('checked', true); // will check the checkbox with id check1
$('#check1').prop('checked', false); // will uncheck the checkbox with id check1

This will not trigger any click action defined.

You can also use:

$('#check1').click(); // 

This will toggle the check/uncheck for the checkbox but this will also trigger any click action you have defined. So be careful.

EDIT: jQuery 1.6+ uses prop() not attr() for checkboxes checked value


$('#check1').prop('checked', true).uniform(); 
$('#check1').prop('checked', false).uniform(); 

This worked for me.


If you are using uniform 1.5 then use this simple trick to add or remove attribute of check
Just add value="check" in your checkbox's input field.
Add this code in uniform.js > function doCheckbox(elem){ > .click(function(){

if ( $(elem+':checked').val() == 'check' ) {
    $(elem).attr('checked','checked');           
}
else {
    $(elem).removeAttr('checked');
}   

if you not want to add value="check" in your input box because in some cases you add two checkboxes so use this

if ($(elem).is(':checked')) {
 $(elem).attr('checked','checked');
}    
else
{    
 $(elem).removeAttr('checked');
}

If you are using uniform 2.0 then use this simple trick to add or remove attribute of check
in this classUpdateChecked($tag, $el, options) { function change

if ($el.prop) {
    // jQuery 1.6+
    $el.prop(c, isChecked);
}

To

if ($el.prop) {
    // jQuery 1.6+
    $el.prop(c, isChecked);
    if (isChecked) {
        $el.attr(c, c);
    } else {
        $el.removeAttr(c);
    }

}

First of all, checked can have a value of checked, or an empty string.

$("input:checkbox").uniform();

$('#check1').live('click', function() {
    $('#check2').attr('checked', 'checked').uniform();
});

Just do this:

$('#checkbox').prop('checked',true).uniform('refresh');

The other way to do is is,

use $('#check2').click();

this causes uniform to check the checkbox and update it's masks