6

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

user3417953
  • 113
  • 2
  • 3
  • 9

3 Answers3

4

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'});
    }
 });
Community
  • 1
  • 1
  • I tried all of those above and for some reason is not working, what is working fine is slide down when is checked one or more, but is not working when UNCHECK or there is NO CHECKED to slide up is not working does any one can help me how to solve when is not checked. – user3417953 Mar 21 '14 at 00:59
  • You can use for states `.addClass('on')`, `.removeClass('on')`, check if `.hasClass('on')`... You can use Your own variable like `var $is_checked = false`, checkbox on change `$is_checked = true`. Please try to write path to Your checkboxes (I do not see Your HTML structure and what You want to do) like `$('div#header div.yourClassBox input:checkbox')` – Laguna Web Design Mar 21 '14 at 10:12
  • i have tried that with .hasclass('on') and ('off') is not working.. i went back to square one and first part is working but second when there is NO checked is not working to slideup please check at [link](http://jsfiddle.net/jasonred/s8Xju/) – user3417953 Mar 21 '14 at 15:17
  • on jsfiddle replace `$('#checkbox')` with `$(':checkbox:checked')` – Laguna Web Design Mar 22 '14 at 12:01
  • To: Laguna Web Desing thanks so much for your help, is works great. – user3417953 Mar 22 '14 at 14:38
1

To check a checkbox:

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

To uncheck a checkbox:

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

To check whether particular checkbox is checked or not:

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

This statement returns true if checked and returns false if unchecked.

m00am
  • 5,910
  • 11
  • 53
  • 69
Rajesh Grandhi
  • 378
  • 3
  • 13
0

This is a bit late but I thought I would put some jquery code that I like to use when checking to see if a box is checked or unchecked. The above answer should work fine, but if you don't want to use .length for some reason you can use this.

to see if a checkbox is checked I use:

$("#checkboxID").is(':checked') - returns true - false    

so to see if a checkbox is not checked I just do this:

!($("#checkboxID").is('checked') - returns true - false but inverses it

Im not sure if this is bad practice or anything along those lines but it works perfect for me.

Imgoinghamm
  • 53
  • 1
  • 6