Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2003, 06:14 AM
Authorized User
 
Join Date: Nov 2003
Location: Redcar, Cleveland, United Kingdom.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help! problems with functions and forms

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

 
Old December 12th, 2003, 06:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old December 12th, 2003, 07:14 AM
Authorized User
 
Join Date: Nov 2003
Location: Redcar, Cleveland, United Kingdom.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Phil

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

Regards
Toka

 
Old December 12th, 2003, 10:29 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Two more things.
Code:
if (SUS >= 45 && SUS<= "100")
should be
Code:
if (SUS >= 45 && SUS<= 100)
you are comparing a number with a string.
Code:
parseInt(SUS)
should be
Code:
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
Code:
javascript:alert(parseInt(070, 10))
compared to
Code:
javascript:alert(parseInt(070))



--

Joe
 
Old December 12th, 2003, 11:09 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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 :D

rgds
Phil
 
Old December 12th, 2003, 11:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old December 16th, 2003, 11:15 AM
Authorized User
 
Join Date: Nov 2003
Location: Redcar, Cleveland, United Kingdom.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks All

Toka




Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with MDI Forms danielkelly C# 4 December 22nd, 2008 11:39 PM
Validating Forms Problems encohen Access 2 July 3rd, 2006 07:30 AM
Problems with Inherited Forms in VC# mmwaikar VS.NET 2002/2003 2 July 20th, 2005 10:01 AM
Problems with string functions and Access 2000 giggs Access VBA 5 February 25th, 2004 09:43 AM
problems between sub and functions charlie Excel VBA 2 July 11th, 2003 01:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.