Stupid Javascript question

Devin Whalen devin-Gq53QDLGkWIleAitJ8REmdBPR1lH4CV8 at public.gmane.org
Mon Feb 14 16:30:16 UTC 2005


On Mon, 2005-02-14 at 09:22 -0500, Jim Skehill wrote:
> Our you sure you have that right? 
> 
> I note that you change the variable name from a to A, but, in javascript,
> variable names are case-sensitive so this will generate an error. 
> 
> I typed up the following html document as a test.
> <html>
> <head>
> 	<title>Add Test</title>
> 	<script language="javascript" type="text/javascript">
> 		function AddTest(input) {
> 			a = 2;
> 			input.value = a+a;
> 		}
> 	</script>
> </head>
> 
> <body>
> 	<table >
> 	<tr >
> 	<td class="wizLabel" >
> 		Add Test
> 	</td >
> 	<td >
> 		<input name="JAVASCRIPT_TEST"  value="42"  class="wizInput"
> onchange="AddTest(this)" >
> 	</td >
> 	</tr >
> 	<tr >
> 	<td class="wizLabel" >
> 		Move Here
> 	</td >
> 	<td >
> 		<input name="MOVE_HERE"  value="42"  class="wizInput">
> 	</td >
> 	</tr >
> 	</table >
> </body>
> </html>
> 
> Place the cursor on the edit labelled Add Test , type in anything, then move
> away (e.g. to the edit labelled Move Here). The contents of the first edit
> gets replaced with the result calculated by the javascript function AddTest.
> On both IE and Firefox the result is 4.
> 
> BTW if you replace the line: 
> 	a = 2: 
> with
> 	a = '2': 
> then you get 22.
> 
> Regards,
> Jim.
> 

You are right, replacing a ='2' with a=2 will work.  However, I have
rarely written a javascript function where I hard-code two variables and
add them together.  The majority of the time my calculations are based
on user input in text boxes or select boxes.  So try your example but
get your value from the input box.  Any value that comes from input
boxes on a form are always string values.  This is why you have to
"trick" javascript into thinking your string value is a number or else
you will just concat your two values.

Later


-- 
Devin Whalen
Programmer
Synaptic Vision Inc
Phone-(416) 539-0801
Fax- (416) 539-8280
1179A King St. West
Toronto, Ontario
Suite 309 M6K 3C5
Home-(416) 653-3982


Take back the Web with FireFox....a browser you can trust
www.getfirefox.com

   .-.
   /v\    L   I   N   U   X
  // \\  
 /(   )\
  ^^-^^   

--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list