Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output the answer of a if expression to a text field

Status
Not open for further replies.

limitup77

IS-IT--Management
Jun 9, 2003
41
GB
Hi and thanks to anyone who can help, I'm brand new to html and keen to learn, but stuck on what I think is a very simple problem, can anyone point me in the right direction.

I want to return a value to a input field [Answer] based upon what is entered into another input field [width]

I think I have the If expression right, I just can't seem to make the result appear in the Answer field.

Here's the script I have: Thanks again

<SCRIPT LANGUAGE="JavaScript">


function Calculate(form){
var Answer

if(width >= 1 && width <= 110){
Answer = "1"
}
if(width >= 111 && width <= 160){
Answer = "1.5"
}
if (width >=161 && width <= 200){
Answer = "2"
}
if (width >=201 && width <= 240){
Answer = "2.5"
}
if (width >=241 && width <= 280){
Answer = "3"
}
// end of JavaScript functions -->
</SCRIPT>
</HEAD>

<BODY>

<P><FONT SIZE="+2">Simple Adder</FONT></P>

<FORM NAME="Calculator" METHOD="post">
<P>Enter a number: <INPUT NAME="width" TYPE=TEXT id="width" SIZE=10>
</P>
<P>
<INPUT TYPE="button" VALUE="Calculate" name="ClearButton" onClick="Calculate(this.form)">
</P>
<P>Answer = <INPUT TYPE=TEXT NAME="Answer" SIZE=12></P>
</FORM>

</BODY>
</HTML>
 
Your code never accesses the form elements to work with the values. As well, your function has no closing brace. Try something like this
Code:
function Calculate(form)
{
var Answer = 0;
var width = form.elements['width'].value;
 
if(width >= 1 && width <= 110)
  {Answer = "1";}
else if(width >= 111 && width <= 160)
  {Answer = "1.5";}
else if (width >=161 && width <= 200)
  {Answer = "2";}
else if (width >=201 && width <= 240)
  {Answer = "2.5";}
else if (width >=241 && width <= 280)
  {Answer = "3";}

form.elements['Answer'].value = Answer;
}

If your calculations followed some kind of formula, I'd recommend using the formula, but the values you use don't progress mathematically.

Is this homework?

Lee
 
Trollacious, thank you very, very much for that...it's been driving me mad. Not homework but a addition to a website someone else built for me.
 
limitup77, how much more HTML coding will you have to do for your job?

By the way, I'm super glad to see that you attempted the javascript on your own. Shows me that not everybody is lazy and just wanting code written for them.

(comment based off yesterday's post in the HTML forum)

[2thumbsup]



[small]"There's an old saying in Tennessee — I know it's in Texas, probably in Tennessee — that says, fool me once, shame on — shame on you. Fool me — you can't get fooled again." - George W. Bush[/small]
<.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top