Reputation: 123
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
Upvotes: 7
Views: 27863
Reputation: 378
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.
Upvotes: 1
Reputation: 53
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.
Upvotes: 0
Reputation: 332
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?
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'});
}
});
Upvotes: 4