Created
June 13, 2012 15:42
-
-
Save Kieranties/2924879 to your computer and use it in GitHub Desktop.
TabChoices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
function checkboxlimit(checkgroup, limit){ | |
var checkgroup=checkgroup | |
var limit=limit | |
for (var i=0; i<checkgroup.length; i++){ | |
checkgroup[i].onclick=function(){ | |
var checkedcount=0 | |
for (var i=0; i<checkgroup.length; i++) | |
checkedcount+=(checkgroup[i].checked)? 1 : 0 | |
if (checkedcount>limit){ | |
alert("You can only select a maximum of "+limit+" checkboxes") | |
this.checked=false | |
} | |
} | |
} | |
} | |
</script> | |
<form method="post" id="thanks" name="thanks"> | |
<!--If I change "tabChoices[]" to "tabChoices" (without the array) it works fine --> | |
<input type="checkbox" name="tabChoices[]" value="EXAMPLE 1" id="tabChoices_1"/> | |
<input type="checkbox" name="tabChoices[]" value="EXAMPLE 2" id="tabChoices_2"/> | |
<input type="checkbox" name="tabChoices[]" value="EXAMPLE 3" id="tabChoices_3"/> | |
</form> | |
<!-- | |
// document.forms.thanks.tabChoices doesn't work (will be undefined) as the checkboxes are called "tabChoices[]" | |
// javascript objects can be accessed using dot notation or like so: | |
--> | |
<script type="text/javascript">checkboxlimit(document.forms.thanks["tabChoices[]"], 2)</script> | |
<!-- | |
// Still, the best thing would be to rename the collection. [] denotes an array in jaavscript and looks confusing as a property/object name | |
--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment