onclick event calling javascript function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

onclick event calling javascript function

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Javascript != PHP

Moved.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post 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
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

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