Ultra fast cheap  linux and windows hosting
javascript tutorial Browse Basic JavaScript Tutorial
Browse JavaScript Script resources
Browse javascript FAQS
Categories:

javascript tutorial
buy template
Home >> Tutorials >> Advanced javascript tutorials
Javascript >> checkbox

Javascript - Limit CheckBox Items Checked

Learn how to limit the number of check box selected using javascript. This will be helpful when you want to give limited option to your customers.
This example script limit only 2 items to be selected. It does not matter which 2. It keeps track of how many check boxes are selected and when the MAX_ALLOWED is reached no more may be choosen until some are deselected.

Item 1
Item 2
Item 3
Item 4


<script language=JavaScript> var max_count=2;
var clickedData=new Array(false,false,false,false);
function itemsClicked(){var i=0;for(var j=0;j<clickedData.length;j++)i+=clickedData[j]?1:0;return(i);}
function itemClicked(t)
{
var check_this=true;
var x=itemsClicked();
if (x>=max_count && !clickedData[t])
{
check_this=false;
}
else
{
clickedData[t]=clickedData[t]?false:true;
eval("document.test.check"+t+".clicked=false;");
}
return (check_this);
}
</script>
<form name=test>
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">
<INPUT name=check1 type=checkbox value=Charity_profile onclick="return itemClicked(1)"> Item 1
<INPUT name=check2 type=checkbox value=lost_found onclick="return itemClicked(2)"> Item 2
<INPUT name=check3 type=checkbox value=fun_section onclick="return itemClicked(3)"> Item 3
<INPUT name=check4 type=checkbox value=contacts onclick="return itemClicked(4)"> Item 4
</form>

Page contents: itechies.net - Learn how to Limit CheckBox Items Checked using javascript.
javascript tutorial
javascript book
Buy this book