onclick event calling javascript function
Posted: Tue Mar 28, 2006 8:21 pm
I am trying to monitor the state of checkboxes on a form. The checkboxes are added to the form from within a php script based on the options contained in a database. I want to have code on the client to indicate status of picks, and limit the number of selected checkboxes.
At this point, I'm exploring with the following test code, which I thought would display the state of the 4th checkbox - but found the value remained unchanged:
The php script that populates the form, and processes the form has no trouble evaluating the state of the checkboxes.
Can I interogate the value of the checkboxes from within the onclick event code, or does the onclick occur prior to the checkbox state being changed? Does CheckMax need to return "true" for the checkbox value to change.
Is there a bettter way?
My end goal is to tally the number of selected options, and provide a warning when the max allowable picks have been exceeded.
I'm thining of something like:
Would this general approach work? Can I walk the object model in this fashion, or is there a better way. I know I didn't transform the index i to a character prior the evaluate - don't know if strval() is appropriate for java.
Thanks in advance,
Michael
At this point, I'm exploring with the following test code, which I thought would display the state of the 4th checkbox - but found the value remained unchanged:
Code: Select all
<script language="JavaScript">
function CheckMax() {
alert("Picked something: " + document.forms[0].chk3.value);
}
</script>Can I interogate the value of the checkboxes from within the onclick event code, or does the onclick occur prior to the checkbox state being changed? Does CheckMax need to return "true" for the checkbox value to change.
Is there a bettter way?
My end goal is to tally the number of selected options, and provide a warning when the max allowable picks have been exceeded.
I'm thining of something like:
Code: Select all
function CheckMax(tnWhoCalled) {
j=0;
for (i=0; i<document.forms[0].TotalOptions.value; i+=1) {
eval('lnNextControlValue = document.forms[0].chk' + i + '.value') ;
if (lnNextControlValue > 0){
j=j+1;
}
}
if (document.forms[0].MaxPicks.value >= j) {
document.forms[0].StatusNumPicks.value = j ;
return true ;
}
else{
eval("document.forms[0].chk" + tnWhoCalled + ".value='0'") ;
return false ;
}
}
</script>Thanks in advance,
Michael