X
Publisher: Sigit purnomo kangsigit.com@gmail.com

Define Limits Voted on Checkbox Using JavaScript

I am very happy to share with you through this blog, especially if you like it. At this post, I had prepared source code to create limits checkbox. It is useful to control options that you set up on the form.

By default, I set it to three checked. Of course you can change it according to their individual needs.
<form id="setOption" name="setOption">
<input type="checkbox" name="state" /> USA<br />
<input type="checkbox" name="state" /> Indonesian<br />
<input type="checkbox" name="state" /> Japan<br />
<input type="checkbox" name="state" /> China<br />
<input type="checkbox" name="state" /> France<br />
</form>
<script type="text/javascript">
/*******
** Get more script: http://www.askedboss.com **
   *************************************************
*****/
function checkboxlimit(optionGroup, limit){
var optionGroup=optionGroup
var limit=limit
for (var i=0; i<optionGroup.length; i++){
optionGroup[i].onclick=function(){
var checkedState=0
for (var i=0; i<optionGroup.length; i++)
checkedState+=(optionGroup[i].checked)? 1 : 0
if (checkedState>limit){
alert("You can only select a maximum of "+limit+" checkboxes") // notif
this.checked=false
}
}
}
}
// Especially for which it should be placed under a checkbox
checkboxlimit(document.forms.setOption.state, 3) // set of three
</script>

Test this code
Advertisement

no image
Add Comments
0 Comments
Click Here to Add Comments

No Comment