Blog: Tag Archives: checkboxes

Javascript – Limit Number of checkboxes

Posted On:

<head>
<title>Katy Whitton Web Development - Limit Checkboxes</title>
<script language="JavaScript">
var ItemCount=0 
var MaxItemCount=2

function setItems(item)
{ 
  if(item.checked)
  {ItemCount=ItemCount+1}

else
 {ItemCount=ItemCount-1} 

if (ItemCount>MaxItemCount)
  {item.checked=false 
  ItemCount=ItemCount-1 
  alert('You may only choose '+MaxItemCount+' Industries') 
  } 
} 
</script>
</head>
<body><br />Select a Maxmimum of 2 items:
<form name="form" target="blank">
Item 1 <input type="checkbox" name="item1" value="item1" onclick="setItems(this)"><br />
Item 2 <input type="checkbox" name="item2" value="item2" onclick="setItems(this)"><br />
Item 3 <input type="checkbox" name="item3" value="item3" onclick="setItems(this)"><br />
Item 4 <input type="checkbox" name="item4" value="item4" onclick="setItems(this)"><br />
Item 5 <input type="checkbox" name="item5" value="item5" onclick="setItems(this)"><br />
</form>
</form>
</body></html>