Page 1 of 1

onclick event calling javascript function

Posted: Tue Mar 28, 2006 8:21 pm
by Michael_C
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:

Code: Select all

<script language="JavaScript">
function CheckMax() {
  alert("Picked something: " + document.forms[0].chk3.value);
}
</script>
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:

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>
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

Posted: Tue Mar 28, 2006 8:37 pm
by feyd
Javascript != PHP

Moved.

Posted: Wed Mar 29, 2006 2:33 am
by RobertGonzalez
This little snippet checks to see if a checkbox is checked and then alerts you. Play around with this and see if you can use it...

Code: Select all

<script language="JavaScript">
<!--//
function processChecks( obj ) { 
	if (obj.form.check_box_name.checked) {
		alert("You clicked a box!");
	}
}
//-->
</script>

<form>
this is a checkbox &mdash;>
<input type="checkbox" name="check_box_name" onClick="processChecks(this);" />
</form>

Posted: Wed Mar 29, 2006 7:43 pm
by Michael_C
Everah wrote:...

Code: Select all

obj.form.check_box_name.checked
So there is a "checked" property - That helped put me back on track, and the script now walks thru all of the checkboxes.

Any idea what the purpose of the checkbox value property?

Thanks again,
Michael

Posted: Wed Mar 29, 2006 7:47 pm
by Michael_C
Never mind on the question of the checkbox value property. Right after I submitted the question I remembered the value is aimed more for displaying/storing something associated with the checkbox.

This environment is so different from what I'm used to.

Michael