jquery checkbox is unchecked
Asked Answered
P

3

7

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

Photoneutron answered 20/3, 2014 at 21:31 Comment(0)
B
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'});
    }
 });
Burly answered 20/3, 2014 at 21:58 Comment(5)
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.Photoneutron
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')Burly
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 linkPhotoneutron
on jsfiddle replace $('#checkbox') with $(':checkbox:checked')Burly
To: Laguna Web Desing thanks so much for your help, is works great.Photoneutron
G
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.

Geodesy answered 6/10, 2016 at 12:3 Comment(0)
K
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.

Kathyrnkati answered 22/1, 2015 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.