<form id="form1" name="form_check" method="post" action="">
<p align="center">
<input type="checkbox" name="role[]" value="1">
<input type="checkbox" name="role[]" value="2">
<input type="checkbox" name="role[]" value="3"></p>
<input type="submit" name="Submit" onclick="check_all()">
<script>
function check_all(){
checkedBox="x"
for(var i=0;i<document.getElementsByName('role[]').length;i++){
if(document.getElementsByName('role[i]').checked == true){
checkedBox="y"
break // No need to check the rest since only one can be checked.
}
}
if(checkBox == "x"){
alert("Checkbox not checked")
}
}
</script>
I have written the code above to check is the checkboxes are checked. It doesn't execute the if statement in the JavaScript area. I am unable to get a output. in JSfiddle when iam trying to execute this script i get this error :
{"error": "Shell form does not validate{'html_initial_name': u'initial-js_lib', 'form': <mooshell.forms.ShellForm object at 0x22c58d0>, 'html_name': 'js_lib', 'html_initial_id': u'initial-id_js_lib', 'label': u'Js lib', 'field': <django.forms.models.ModelChoiceField object at 0x25563d0>, 'help_text': '', 'name': 'js_lib'}{'html_initial_name': u'initial-js_wrap', 'form': <mooshell.forms.ShellForm object at 0x22c58d0>, 'html_name': 'js_wrap', 'html_initial_id': u'initial-id_js_wrap', 'label': u'Js wrap', 'field': <django.forms.fields.TypedChoiceField object at 0x2556cd0>, 'help_text': '', 'name': 'js_wrap'}"}
This script worked for me:
var radios = document.getElementsByName('role[]');
checkedBox = "x";
for (i = 0; i < radios.length; i++) {
if (radios[i].checked) {
//alert("checked: " + radios[i].value);
checkedBox = "y";
break; // No need to check the rest since only one can be checked.
}
}