Subject: Help! problems with functions and forms
Posted By: Toka1 Post Date: 12/12/2003 5:14:14 AM
I am very new to javascript, I am trying to write a piece of code that will take input from a textbox and do certain calculations depending on the input.
my confusion is with the function, I don't quite get the parameters bit and I think I have missed something relating to this on the form in the function()
Please could someone point me in the right direction, here is my code so far.

<html>
<head>
<title>Calculator</title>





<script language="JavaScript">



function butCheckForm_onclick()

{

var myForm = document.form1;

var Result;

var SUS=myForm.txtSaybolt.value

SuS=parseInt(SUS);


var Result= return Result;


if (SUS >= 45 && SUS<= "100")
{
    Result=SUS*0.2253;
}
else if (SUS>=100 && SUS<=240)
{
    Result=SUS*.02193;
}

return Result;

}
alert(Result);


</script>

</head>

<body>

<form name=form1>

<p><pre>
Please Enter SUS
<input type="text" name="txtSaybolt" value=""></input>
<input type="button" value="Check Details"
Name=butCheckForm onclick="butCheckForm_onclick()"></input>
</pre></p>

</form>
</body>
</html>

Thank you in advance

Toka

Reply By: pgtips Reply Date: 12/12/2003 5:57:45 AM
Just a bit of syntax problem:

var Result= return Result;
take out this line, its nonsense.  You've already declared the result variable, and anyway you can't use the return keyword on the RHS of an assignment.

alert(Result);
take out this line too.  the variable Result only exists inside the body of the function, so it doesn't exist when you're trying to alert it.  If you want to see the return value use <input type="button" value="Check Details"
Name=butCheckForm onclick="alert(butCheckForm_onclick());">

SuS=parseInt(SUS);
js is case-sensitive so you need to change this to
SUS=parseInt(SUS);

hth
Phil
Reply By: Toka1 Reply Date: 12/12/2003 6:14:31 AM
Thanks Phil

That worked a treat, and thank you for explaining what I had done wrong and why it wouldn't work.

Regards
Toka

Reply By: joefawcett Reply Date: 12/12/2003 9:29:53 AM
Two more things.

if (SUS >= 45 && SUS<= "100")

should be

if (SUS >= 45 && SUS<= 100)

you are comparing a number with a string.

parseInt(SUS)

should be

parseInt(SUS, 10)

You must specify the base of the target system or you will fall foul of someone typing in 070 for 70.
Try typing the following into the browser address bar and pressing enter

javascript:alert(parseInt(070, 10))

compared to

javascript:alert(parseInt(070))





--

Joe
Reply By: pgtips Reply Date: 12/12/2003 10:09:05 AM
Nice one Joe

I suppose one could also argue that
if (SUS >= 45 && SUS<= "100")
{
    Result=SUS*0.2253;
}
else if (SUS>=100 && SUS<=240)
{
    Result=SUS*.02193;
}
is ambiguous because if SUS = 100 then it satisfies both tests, but that would be too pedantic

rgds
Phil
Reply By: pgtips Reply Date: 12/12/2003 10:12:58 AM
LOL, Joe you forgot the quotes too.  Both your examples will alert 56!
javascript:alert(parseInt("070", 10)) - alerts 70
javascript:alert(parseInt("070")) - alerts 56

Reply By: Toka1 Reply Date: 12/16/2003 10:15:06 AM
Thanks All

Toka


Go to topic 7625

Return to index page 985
Return to index page 984
Return to index page 983
Return to index page 982
Return to index page 981
Return to index page 980
Return to index page 979
Return to index page 978
Return to index page 977
Return to index page 976