# Simple Javascript question



## yoyo1234 (Dec 24, 2003)

I am trying to write what should be an incredibly easy script and cannot get it working on either Mozilla or IE. 

Description:
1. User enters a number in the first textfield representing ounces.
2. User clicks out of the textfield and the number is converted from ounces to grams in a second textfield.

Sounds easy, but I cannot get it working for some reason. Can someone help me. I plan on adding approx 5 or 6 more conversions to the same form.

Below is the html I have been using:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript" language="Javascript">
	function calc(val,factor,result) {
	test = val.value*factor
	test2 = test.toPrecision(4)
	document.forms.converter.result.value = test2
	}
</script> 
</head>
<body>
<form name="converter">
	<input name="floz" type="text" onchange="calc(this, 28.349523125, 'grams')"> oz
	<input name="grams" type="text"> 
</form>
</body>
</html>


----------



## Raptur (Dec 25, 2003)

Try the following:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
         "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <title>Untitled</title>
 <script type="text/javascript" language="Javascript">
         function calc(val,factor,result) {
         test = val.value*factor
         test2 = test.toPrecision(4)
         result.value = test2
         }
 </script> 
 </head>
 <body>
 <form name="converter">
         <input name="floz" type="text" onchange="calc(this, 28.349523125, document.converter.grams)"> oz
         <input name="grams" type="text"> 
 </form>
 </body>
 </html>


You can't pass a text string and then use that to access a form element; the browser was looking for a form element called "result". Instead, pass the actual form element itself. 

And here's a tip for javascript programming: use mozilla. If you have problems, type "javascript:"(without the underscores, for some reason the board adds underscores) in the location bar, and a handy debugger pops up. Usually tells you exactly where the problem is.

-=-raptur-=-


----------



## Arden (Dec 25, 2003)

Remember... you must end any lines that don't start a block of code in curly brackets with a semicolon, including your call to onchange.


----------



## yoyo1234 (Dec 25, 2003)

Your suggestions worked, but it only works in Mozilla. IE and Safari do not pass the numbers correctly. 

I have added a link here showing my problem.

Using either IE or Satari, enter a number in any of the textfields on the left and click one of the textfields on the right. Notice the converted number is not being displayed on the right.

Please help


----------



## Arden (Dec 25, 2003)

You apparently didn't listen to me.  You should do your JS script like this:

```
<script type="text/javascript" language="Javascript">
function calc(val,factor,result) {
	test = val.value*factor;
	test2 = test.toPrecision(4);
	result.value = test2;
}

function SGConvert(val,result) {	
	test = (val.value - 1) / .0074	;
	test2 = test.toPrecision(4);
	result.value = test2;
}

</script>
```
Note the semicolons.  Without these, the browser will think all your lines of code are just one line and it won't interpret them correctly.


----------



## yoyo1234 (Dec 25, 2003)

Thanks. I forgot to add the ";" 

I've added them, but i am still seeing odd behavior:
1. Works fine in Mozilla v1.3 (Mac) and v1.4 (Win XP Pro)
2. Works fine in IE 6 (Win XP Pro)
3. Does *not* work in IE (v5.23 Mac)
4. Does *not* work in Safari(v1.1 Mac).

Any other suggestions?


----------



## yoyo1234 (Dec 26, 2003)

I have been narrowing down the problems, and discovered it is the toPrecision() and toFixed() methods that are causing my problem. Once I commented that line out, everything works in all my browsers. After further testing I think it may be a defect in the WebKit core used in Safari and Omniweb 4.5. Therefore, I was wondering if someone else is able to recreate my problem?

Can someome try to figure out how to use the toPrecision() and toFixed() methods in Safari or OmniWeb 4.5?

Here  is the most current version of the document I am testing with. 



			
				yoyo1234 said:
			
		

> Thanks. I forgot to add the ";"
> 
> I've added them, but i am still seeing odd behavior:
> 1. Works fine in Mozilla v1.3 (Mac) and v1.4 (Win XP Pro)
> ...


----------



## Arden (Dec 26, 2003)

This page should give you some insight on rounding your numbers to so many decimal places (I wouldn't go more than 2 or 3).

Also, you should set it up so the conversion will work backwards, so I could convert from grams to ounces as well as vice versa.


----------



## yoyo1234 (Dec 26, 2003)

I have compiled all of your suggestions and came up with this. 

My goal was to create an idiot-proof unit converter for my homemade wine website. I also needed a way for a person to be able to display more than one conversion at the same time. Most of my users are not very computer savy so I wanted make it as easy to use as possible; i.e. no menus, buttons, etc. I realize it still needs some visual work with CSS, but it functions as I wanted/expected.

Here are the modifications I added:
1. All of the textfields on the right are read only on purpose to protect the conversion results and force people to enter numbers in the textfields on the left.
2. Added tabindex numbers to the textfields on the left so they tab in a logical order.
3. Rounded all the results to the .xx decimal place.

Thanks again for all of your assistance  


Comment? Suggestions on improvements?


----------



## Arden (Dec 27, 2003)

Awesome!  It works quite well.  The only thing I'd add, if possible, would be something that would do the conversion as they enter it, but that's minor since it will show up as soon as they do anything else.

Keep on truckin'!


----------



## yoyo1234 (Dec 27, 2003)

Arden said:
			
		

> The only thing I'd add, if possible, would be something that would do the conversion as they enter it.




That's a great idea, and I found a way to to it. Check it out and tell me what you think.


----------



## Arden (Dec 27, 2003)

Wonderful!  Now you must package and sell your software.


----------

