Checkbox To Act Like Radio Button, Check First Four Or Last Four Checkboxes But Not Both- Javascript?
I am having a problem in javaScript. I want to check either any of the first 4 checkboxes or any of the last four checkboxes to true. here in my code if i check any one of the fi
Solution 1:
Consider using the the 'checked' parameter of you checkOnly function to determine which checkbox the user checked (maybe give them unique ids). As your logic stands now, if you check the first checkbox, it will clear out the checkboxes on the last four. Then if you check the last checkbox, it will still clear out the last four checkboxes because the first checkbox is still checked. This makes it look like the last four checkboxes are disabled even though they aren't.
For example:
<html><head><title>FooBar</title><scriptlanguage="javascript">functioncheckOnly(myCheckbox) {
var checkboxChanged = false;
var checkedTotalValue = 0;
var changedTotalValue = 0;
for(var i = 0; i < document.myForm.elements.length; i++) {
if(document.myForm.elements[i].name != myCheckbox.name) {
if(document.myForm.elements[i].checked == true) {
checkboxChanged = true;
changedTotalValue += parseInt(document.myForm.elements[i].value);
}
document.myForm.elements[i].checked = false;
}
if(document.myForm.elements[i].checked == true) {
checkedTotalValue += parseInt(document.myForm.elements[i].value);
}
}
if(checkboxChanged) {
alert('Checked: ' + checkedTotalValue + ', Changed: ' + changedTotalValue);
}
}
</script></head><body><formname="myForm"><inputtype="checkbox"name="checkboxGroup1"id="cb"value="1"onClick="checkOnly(this)"><inputtype="checkbox"name="checkboxGroup1"id="cb"value="1"onClick="checkOnly(this)"><inputtype="checkbox"name="checkboxGroup1"id="cb"value="1"onClick="checkOnly(this)"><inputtype="checkbox"name="checkboxGroup1"id="cb"value="1"onClick="checkOnly(this)"><inputtype="checkbox"name="checkboxGroup2"id="cb"value="1"onClick="checkOnly(this)"><inputtype="checkbox"name="checkboxGroup2"id="cb"value="1"onClick="checkOnly(this)"><inputtype="checkbox"name="checkboxGroup2"id="cb"value="1"onClick="checkOnly(this)"><inputtype="checkbox"name="checkboxGroup2"id="cb"value="1"onClick="checkOnly(this)"></form></body></html>
Post a Comment for "Checkbox To Act Like Radio Button, Check First Four Or Last Four Checkboxes But Not Both- Javascript?"