Reputation: 51
I am using checkboxes in my application and I have a problem that I haven't been able to solve despite my efforts.
function allChecked(status,set){
alert(set);
if(status)
$('input[type=checkbox]').attr('checked','true');
else
$('input[type=checkbox]').attr('checked','');
}
The above function selects or de-selects all checkboxes depending on the value of "status". "set" is a number that specifies how many checkboxes should be selected. So I want the code to select the checkboxes depending on value of "set". How do I do this?
Upvotes: 2
Views: 1875
Reputation: 10896
try something like this
function allChecked(status,set){
alert(set);
if(status)
$('input[type=checkbox]:lt('+set+')').attr('checked','true');
else
$('input[type=checkbox]:lt('+set+')').attr('checked','');
}
Upvotes: 0
Reputation: 9954
EDIT :
For Function call you can use below code
function allChecked(){
if($('#selectall').attr('checked', this.checked))
$(".case").attr("checked", "checked");
else
$(".case").removeAttr("checked");
}
Example : http://jsfiddle.net/ipsjolly/wUgwz/7/
You can also use ID instead of calling a function
$(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");
}
});
});
Example : http://jsfiddle.net/ipsjolly/wUgwz/8/
Source : http://viralpatel.net/blogs/multiple-checkbox-select-deselect-jquery-tutorial-example/
Upvotes: 1
Reputation: 10795
Assuming you want to check or un-check the first N checkboxes (in the order in which they appear in the DOM), you can use the slice method to accomplish this:
function allChecked(status, set) {
var checkboxes = $('input[type=checkbox]').slice(0, set);
if (status) {
checkboxes.attr('checked', 'checked');
}
else {
checkboxes.removeAttr('checked');
}
}
Demo: http://jsfiddle.net/brianpeiris/Xs9Vc/
Upvotes: 1