PDA

View Full Version : converter


chrismiceli
09-10-2002, 11:27 PM
I wrote a script to convert quarts to liters (times the quarts by 1.06 or vice versa) well anyway here is the script, the error is mult is undefined.

<html>
<head>
<title>Converter</title>
<script language="javascript">
function usql() {
document.form.input.value = mult;
answer = eval(mult * 1.06)
document.form.result.value = answer
}
</script>
</head>
<body>
<form>
<input type="text" name="input" value="">
<input type="text" name="result" value="">
<p>
<input type="button" name="ql" value="Quart to Liter" onClick="usql(mult)">
</form>
</body>
</html>

A1ien51
09-10-2002, 11:34 PM
first off, i would not use input and result as the form names, since they are resevered words in java script/html and you might give netscape a heart attack

second......

document.form.input.value = mult;

should be

var mult = document.form.input.value;

A1ien51

chrismiceli
09-10-2002, 11:48 PM
it still doesn't become defined!

ConfusedOfLife
09-10-2002, 11:56 PM
<html>
<head>
<title>Converter</title>
<script language="javascript">
function usql(mult)
{
answer = parseInt(mult) * 1.06;
document.forms[0].result.value = answer ;
}
</script>
</head>
<body>
<form>
<input type="text" name="Oopse" value="">
<input type="text" name="result" value="">
<p>
<input type="button" name="ql" value="Quart to Liter" onClick="usql(this.form.Oopse.value)">
</form>
</body>
</html>

chrismiceli
09-11-2002, 12:28 AM
could you explain how that works please.

ConfusedOfLife
09-11-2002, 11:04 PM
whenever you have a form and you wana send one of it's inputs' values to a function ( for example in your case, the value of a text input named Oopse), you can send that value by typing:

this.form.NameOfYourElement.value

And you have to use this in the form, I mean for example in the onclick event of one of the buttons OF the form. You can also refer to any value of a form from outside of that form ( like in a function ) by having :

document.FormName.element'sName.value.

The things that are written in bold, should be completed by you, depending on names that you assigned to your elements. For example, instead of typing

<input type="button" name="ql" value="Quart to Liter" onClick="usql(this.form.Oopse.value)">

and then using mult in the function, I could have changed the function to get the Oopse value itself without having any argument, then we had:


function usql()
{
mult = document.forms[0].Oopse.value;
answer = parseInt(mult) * 1.06;
document.forms[0].result.value = answer ;
}


And I'm not gonna write that this.form.Oopse.value in the onclick of my button, coz it's no need to do that and also as you can see, I no longer have an argument in my function ( the new function that I defined above )

You might ask what that forms[0] is, and I would say that instead of naming all the forms in your document, you can use the forms array, then forms[0] is gonna be the first form that you used in your document, and forms[n] is gonna be the nth form that you used in your document. We also have the elements array. As you guessed, instead of naming all the elements of your form, you can refer to them based on their order, for example our Oopse in here is elements[0]. So, we could have written all the function in this way :

function usql()
{
mult = document.forms[0].elements[0].value;
answer = parseInt(mult) * 1.06;
document.forms[0].elements[1].value = answer ;
}


Also it's not a good way to refer to our elements and get their values through a function, unless we're making that function specifically for an element of the form ( we usually don't do this! ), coz of that, sending one or more arguments to a function is more advicable.

Also we have a predefined function is JS called parseInt() that converts a string into an integer, because the value of each text input is a string, I used that to turn my string to a number that I can use it later in the equation. I hope that's enough for now, keep working and you'll discover more!