Reputation: 29
I have a table which displays students record in asp.Now a column in that table contains a check box and also a functionality to check all the check boxes would be provided at the top. How to do that using javascript?
Upvotes: 0
Views: 657
Reputation: 3165
define a link anywhere, grab its event and use checked property
<a href="#" rel="checkedBoxes">Check All Boxes</a>
<a href="#" rel="uncheckedBoxes">Uncheck all Boxes</a>
<script>
$(function() {
$("a[rel=checkedBoxes]").live("click", function(eV) {
eV.preventDefault();
$("form input:checkbox").attr("checked", "checked");
}
$("a[rel=uncheckedBoxes]").live("click", function(eV) {
eV.preventDefault();
$("form input:checkbox").attr("checked", "");
}
});
</script>
Upvotes: 0
Reputation: 6858
see demo
$(function(){
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){
if($(".case").length == $(".case:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
Upvotes: 1