jquery checkbox is unchecked

user3417953 picture user3417953 · Mar 20, 2014 · Viewed 20.4k times · Source

I have pagination that comes with checkbox in first column and each row.

If any of checkbox is checked it will automatic slidedown full wide box from top, I use jquery here is code...

$(':checkbox').change(function() {
    // use the :checked selector to find any that are checked
    if ($('#checkbox').length) {

        $(".checkbox-tools").animate({top: '0px'});

    } else {
        $('.checkbox-tools').slideUp('slow');
    }
 });
  <!--Cancel print button.-->   

$(document).ready(function(){
  $("#cancel-chedkbox").click(function(){
    $(".checkbox-tools").animate({top: '-450px'});
     $('input:checkbox').removeAttr('checked');
  });});

This works great when checked on checkbox and slide down.

What if person UNCHECKED and there is no checked in checkbox and I want to automatic slideup full width box instead of person click close button if there is no checked at all.

How can I solve that!

AM

Answer

Laguna Web Design picture Laguna Web Design · Mar 20, 2014

For uncheck checkboxes use:

$('input:checkbox').prop('checked', false)

To check checkboxes use:

$('input:checkbox').is(':checked')

To count checked checkboxes use:

$('input:checked').length

It's similar post on: checking if a checkbox is checked?

UPDATE:

In Your code, all checkboxes have ID #checkbox - ID have to be unique, here You have working code http://jsfiddle.net/s8Xju/1/:

$(':checkbox').change(function() {
    // use the :checked selector to find any that are checked
    if ($(':checkbox:checked').length > 0) {

        $(".checkbox-tools").animate({top: '200px'});

    } else {

        $('.checkbox-tools').animate({top: '-450px'});
    }
 });