Help - Search - Members - Calendar
Full Version: I'm new...easy question
HTMLHelp Forums > Programming > Client-side Scripting
Eddy
I've got this calculator I'm using on a website...

Simple question: After clicking "Calculate Savings" button, it shows the answer (1250 in this example) in a text box
"1250 dollars in annual savings"

I'd simply like to change it to read:
"You could save up to $1250 per year"

Everytime I try to change the code (in red) it results in an error...can someone help...here's the code:

<HTML>
<HEAD>
</HEAD>
<BODY>
<FORM ACTION="#" NAME=calc>
Purchase Price
<INPUT TYPE=TEXT NAME="originalprice" SIZE=5>
<BR><BR>
Current Market Value
<INPUT TYPE=TEXT NAME="cmv" SIZE=5>
<BR><BR>
Tax Rate (i.e. 1.25)
<INPUT TYPE=TEXT NAME="rate" SIZE=3>
<BR><BR>
<INPUT TYPE=BUTTON VALUE="Calculate Savings"
ONCLICK="document.calc.savings.value
=((document.calc.originalprice.value * document.calc.rate.value)/100)
- ((document.calc.cmv.value * document.calc.rate.value)/100)+ ' dollars in annual savings'">

<BR><BR>
<TEXTAREA NAME=savings
style="
font-size: 16px;
font-weight: bold;
color: white;
background-color: green;"
COLS=30 ROWS=2 ReadOnly>
</TEXTAREA>
</FORM>
</BODY>
</HTML>

Darin McGrew
I've moved this to the client-side scripting forum, since it's a question about JavaScript.
Darin McGrew
Do you have an example (preferably a URL) of your best effort at modifying the onclick event handler?
Christian J
Probably the text makes the JS regard the numbers as part of the string, and thus gives a NaN (not a number) error. If you separate the parts it seems to work:

CODE
<script type="text/javascript">
function foo()
{
    var amount=((document.calc.originalprice.value * document.calc.rate.value)/100)- ((document.calc.cmv.value * document.calc.rate.value)/100);
    document.calc.savings.value='You could save up to $' + amount + ' per year';
}
</script>

<INPUT TYPE=BUTTON VALUE="Calculate Savings"
ONCLICK="foo();">


Eddy
That was exactly what was happening....I appreciate your help...that fixed the problem!! biggrin.gif
dksibert
Change the line to:

<INPUT TYPE=BUTTON VALUE="Calculate Savings" ONCLICK="document.calc.savings.value= '$'+(((document.calc.originalprice.value * document.calc.rate.value)/100)-((document.calc.cmv.value * document.calc.rate.value)/100)+ ' per Year')">

QUOTE(Eddy @ May 1 2008, 05:07 PM) *

I've got this calculator I'm using on a website...

Simple question: After clicking "Calculate Savings" button, it shows the answer (1250 in this example) in a text box
"1250 dollars in annual savings"

I'd simply like to change it to read:
"You could save up to $1250 per year"

Everytime I try to change the code (in red) it results in an error...can someone help...here's the code:

<HTML>
<HEAD>
</HEAD>
<BODY>
<FORM ACTION="#" NAME=calc>
Purchase Price
<INPUT TYPE=TEXT NAME="originalprice" SIZE=5>
<BR><BR>
Current Market Value
<INPUT TYPE=TEXT NAME="cmv" SIZE=5>
<BR><BR>
Tax Rate (i.e. 1.25)
<INPUT TYPE=TEXT NAME="rate" SIZE=3>
<BR><BR>
<INPUT TYPE=BUTTON VALUE="Calculate Savings"
ONCLICK="document.calc.savings.value
=((document.calc.originalprice.value * document.calc.rate.value)/100)
- ((document.calc.cmv.value * document.calc.rate.value)/100)+ ' dollars in annual savings'">

<BR><BR>
<TEXTAREA NAME=savings
style="
font-size: 16px;
font-weight: bold;
color: white;
background-color: green;"
COLS=30 ROWS=2 ReadOnly>
</TEXTAREA>
</FORM>
</BODY>
</HTML>

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.