Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a variable to a function

Status
Not open for further replies.

ideasworking

Programmer
Dec 2, 2001
120
CA
Hello,

I could use a little help with a function. I wrote a function that should update the value in a text box named "CityUsage" depending on the state of a checkbox However the code just isn't working. Can someone help me identify the problem?

Here's my function...
Code:
<script language="JavaScript">
function cc(a)
{
var PreviousValue = 771970    
if (a=1)
{document.forms["WaterReadings"].CityUsage.value= document.forms["WaterReadings"].NewCityWaterReading.value - PreviousValue}
else
{document.forms["WaterReadings"].CityUsage.value= document.forms["WaterReadings"].NewCityWaterReading.value - PreviousValue + 1000000}
}

I call the function from the keyup event of a text box as shown below. I put 1 in the call to the function for testing purposes.

Code:
<input name="NewCityWaterReading" type="text" id="NewCityWaterReading" onKeyUp="cc(1)" value="771970" maxlength="10" align="right">

I think the actual call to the function should be something like this... but maybe not???

Code:
<input name="NewCityWaterReading" type="text" id="NewCityWaterReading" onKeyUp="cc(<?PHP echo "document.forms["WaterReadings"].Checkbox.value" ?>)" value="771970" maxlength="10" align="right">
 
In fact, I'd modify the script to be a bit neater, and add in some semicolon delimiters (not always needed, but should be present, really):

Code:
<script type="text/javascript">
function cc(a) {
	var PreviousValue = 771970;
	var frm = document.forms['WaterReadings'].elements;
	if (a == 1) {
		frm['CityUsage'].value = frm['NewCityWaterReading'].value - PreviousValue;
	} else {
		frm['CityUsage'].value = frm['NewCityWaterReading'].value - PreviousValue + 1000000;
	}
}
</script>

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks for the response. I tried your recommendations and made some changes; still it didn't work. The biggest problem seems to be passing the value of the the checkbox to the function. With the code I have below no alert is raised. If I change the code so that var a = '1' then the alert is raised. Any suggestions?

Thanks,
Lou

Code:
<script language="JavaScript">

function ccw()
{
var a = document.forms['WaterReadings'].['CityWaterRollOver'].value; 
if(a == '1'){
alert ('Hello');
} else {
alert ('Goodbye');
}
}
</script
 
Where did you get this crazy syntax from?

Code:
document.forms['WaterReadings'].['CityWaterRollOver'].value;

It's not legal, and will probably be causing your code to error. Try putting in the syntax I gave you - which works.

You're also missing a closing ">" off of your closing script tag. In addition, you've reverted to using "language" instead of "type" for your JS declaration - a bit outdated, which is why I showed you the newer syntax.

Do you not indent your code? It makes it so much easier to understand - not only for you, but for others (such as people here) to view.

Hope this helps,
Dan





Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Dan,

Thank you very much for your help. Yes the problem is resolved. I'm sure the code could be optimized further... but at this point my focus is on creating a functioning script.

What I found was I needed to directly reference the state of the checkbox in the IF statement. For anyone who may try something like this in the future the code that worked for me is...

Code:
function ccw(){
   var PreviousValue = 771970    ;
   var frm = document.forms["WaterReadings"].elements;

   if (frm["CityWaterRollover"].checked) {
      if (frm["NewCityWaterReading"].value== PreviousValue) {
         frm["CityUsage"].value = frm["NewCityWaterReading"].value - PreviousValue;
      } else {
         frm["CityUsage"].value = frm["NewCityWaterReading"].value - PreviousValue + 1000000;
      }
   } else {
      frm["CityUsage"].value = frm["NewCityWaterReading"].value - PreviousValue;
   }
}


One more thing... I'm actually creating this code with PHP so that the variable PreviousValue changes dynamically. That code is as follows...

Code:
	echo "function ccw(){\n";
	echo "   var PreviousValue = ".$CW.";\n";
   	echo "   var frm = document.forms[\"WaterReadings\"].elements;\n";
	echo "\n";
	echo "   if (frm[\"CityWaterRollover\"].checked) {\n";
	echo "      if (frm[\"NewCityWaterReading\"].value== PreviousValue) {\n";
 	echo "         frm[\"CityUsage\"].value = frm[\"NewCityWaterReading\"].value - PreviousValue;\n";
	echo "      } else {\n";
	echo "         frm[\"CityUsage\"].value = frm[\"NewCityWaterReading\"].value - PreviousValue + 1000000;\n";
	echo "      }\n";
	echo "   } else {\n";
	echo "      frm[\"CityUsage\"].value = frm[\"NewCityWaterReading\"].value - PreviousValue;\n";
	echo "   }\n";
	echo "}\n";
	echo "\n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top