/    Sign up×
Community /Pin to ProfileBookmark

calculation problem

Hi, guys:
I am a JavaScript newbie.
I am trying to do calculations using JavaScript, I try to
get total based on user inputted amount and rate values,
and get amount based on user inputted total and rate; rate value
is actually preset:

function calcTotal(form)
{
var amount = form.amount.value;
var rate = form.rate.value;
var total = form.total.value;

form.total.value = (parseFloat(form.amount.value) * parseFloat(form.rate.value));
}

function calcAmount(form)
{
var rate = form.rate.value;
var total = form.total.value;
var amount = form.amount.value;

form.amount.value = (parseFloat(form.total.value) / parseFloat(form.rate.value));
}

<input type=”text” name=”amount” maxlength=”16″ size=”40″ value=”” onchange=”calcAmount(this.form)”>
</td>

<input type=”text” name=”total” maxlength=”16″ size=”20″ value=”” onchange=”calcTotal(this.form)”>
</td>
<input type=”text” name=”rate” maxlength=”10″ size=”15″ value=”1.50″>

but I keep getting NaN if I input one number into either total textbox
or amount textbox. What did I do wrong?

regards,

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@javaNoobieOct 26.2004 — get total based on user inputted amount and rate values,

and get amount based on user inputted total and rate; rate value[/quote]
You are suppose to call calcTotal when you input amount and call calcAmount when you input total. Swap your functions in your textboxes.
Copy linkTweet thisAlerts:
@rick_colsauthorOct 26.2004 — [i]Originally posted by javaNoobie [/i]

[B]You are suppose to call calcTotal when you input amount and call calcAmount when you input total. Swap your functions in your textboxes. [/B][/QUOTE]


Ahhh.... Many thanks, javaNoobie. I just scratch my head without seeig the obvious....
×

Success!

Help @rick_cols spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...