Home >> Tutorials >> Javascript tutorials

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 chosen 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])
return (check_this); 

<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 

Page contents: itechies.net – Learn how to Limit CheckBox Items Checked using javascript.