Monday, October 26, 2009

Limit the number of selected checkboxes in javascript

If you have multiple checkboxes:

Html Code




Mac OS X
Linux
Windows





You can get the count using this:

JavaScript Code:



var cnt=0

function check(obj,maxs){
if(obj.checked){
cnt=cnt+1
}else{
cnt=cnt-1
}

if (cnt>maxs){
obj.checked=false
cnt=cnt-1
alert('Can only select up to '+maxs)
}
}

No comments: