Here is the updated function to limit the number of checkboxes that can be checked in a fieldset
Code:
function check(f,o,m){
var c = i = 0, b = f.elements;
for (i; i < b.length; i++)
if(b[i].type&&b[i].type=='checkbox'&&b[i].checked&&b[i].parentNode.id==o)
c++;
for (i = 0; i < b.length; i++)
if(b[i].type&&b[i].type=='checkbox'&&!b[i].checked&&b[i].parentNode.id==o)
b[i].disabled=c==m;
}
Just be sure to give the fieldset a unique ID
Code:
<fieldset id="options">
Call the function from the form onclick. Be sure to set the number of checkable checkboxes. In this case I limit it to 1
Code:
<form action="#" id="sampleform" onclick="check(this,'options',1);">
To restrict the number of checkboxes that can be checked in multiple fieldsets include each call in the form's onclick
Code:
<form action="#" id="sampleform" onclick="check(this,'options',1);check(this,'moreoptions',5);">
In the fieldset with the ID of "options" you can check one checkbox. In the fieldset with the ID of "moreoptions" you can check five checkboxes.
Bookmarks