/    Sign up×
Community /Pin to ProfileBookmark

Call event when another field is changed

I have a form that customers input the quantity they want of an item and submit the form to me, and I receive it in an email…

It calculates the total per line, and then the total for the order, but I do not have it calculating all of these fields in a currency format.

I am trying to make it so when the fields are updated, they are converted from 1000.5 to $1,000.50
I know nothing about java script, I am just using code I find and trying to make it work to what I need…

[CODE]<SCRIPT LANGUAGE=”JavaScript”>
function formatCurrency(num) {
num = num.toString().replace(/$|,/g,”);
if(isNaN(num))
num = “0”;
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = “0” + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+’,’+
num.substring(num.length-(4*i+3));
return (((sign)?”:’-‘) + ‘$’ + num + ‘.’ + cents);
}
</script>
<script language=”Javascript”>
function computeAdd90055 (obj) {
obj.res90055.value = eval (obj.val90055.value) *
eval (obj.val90055p.value);
}
function computeAdd21903 (obj) {
obj.res21903.value = eval (obj.val21903.value) *
eval (obj.val21903p.value);
}
function computeAdd90795 (obj) {
obj.res90795.value = eval (obj.val90795.value) *
eval (obj.val90795p.value);
}
function computeAddTotal (obj) {
obj.total.value = eval (obj.res90055.value) +
eval (obj.res21903.value) +
eval (obj.res90795.value);
}

</script>
</head>
<body>
<form>
5-9005: <input type=”text” name=”val90055″ value=”0″ onchange=”computeAdd90055(this.form);computeAddTotal(this.form);”> <br>
Cost: $<input type=”text” name=”val90055p” value=”56.33″> <br>
Total Line Cost: $<input type=”text” value=”0″ name=”res90055″><br><br>

3-2190: <input type=”text” name=”val21903″ value=”0″ onchange=”computeAdd21903(this.form);computeAddTotal(this.form)”> <br>
Cost: $<input type=”text” name=”val21903p” value=”51.32″> <br>
Total Line Cost: $<input type=”text” value=”0″ name=”res21903″><br><br>

5-9079: <input type=”text” name=”val90795″ value=”0″ onchange=”computeAdd90795(this.form);computeAddTotal(this.form)”> <br>
Cost: $<input type=”text” name=”val90795p” value=”55.95″> <br>
Total Line Cost: $<input type=”text” value=”0″ name=”res90795″> <hr>

Total: $<input type=”text” name=”total” onBlur=”this.value=formatCurrency(this.value);”>
</form>[/CODE]

Currently as it stands, if I clicked inside of the total box, and then clicked out of it, the conversion would happen… how to I make it so when this box is populated with numbers, this happens automatically?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@kurbyApr 10.2008 — I would store the total in a global variable and then call formatcurrency everytime you add to it.

Computeaddtotal should just add to the global variable $total, instead of the text box. Plus, the way you have it now, anyone can change the total without changing the items before it.
Copy linkTweet thisAlerts:
@jhayes82687authorApr 11.2008 — When I am finished getting the javascript correct I am going to clean up the page so it looks better... i haven't formatted it at all just testing... im going to disable the line totals and order total fields..

Could you explain further how to store total in a global variable?
Copy linkTweet thisAlerts:
@kurbyApr 11.2008 — What you need to do is store globals for each of your fields. Then call formatcurrency everytime you call a computeAdd function.

[CODE]
<script language="Javascript">
var total;
var 90055Val;
var 21903Val;
var 90795Val;
function computeAdd90055 (obj) {
90055Val = eval (obj.val90055.value) *
eval (obj.val90055p.value);
obj.res90055.value = formatCurrency(90055Val);
}

function computeAddTotal (obj) {
total = Number(90055Val) + Number(21903Val) + Number(90795Val);
obj.total.value = formatCurrency(total);
}

</script>[/CODE]


You can fill in the rest for your other functions.

On another note, all your functions are doing the exact same thing. Perhaps you should look into a way of only using one function, by passing it the 3 text fields you need. Something like this:

[CODE]
var ArrayOfLineTotals = new Array.
function computeLineTotal(itemname, numordered, cost, subTotal)
{
ArrayOfLineTotals[itemname] = numordered.value * cost.value;
subTotal.value = formatCurrency(ArrayOfLineTotals[itemname]);
}
[/CODE]


Then you can call that as many times as you want and it will just add to the array. Then when you call add to total, it needs to iterate through the array adding all the values together. If that doesn't make sense, just go with the original solution.
×

Success!

Help @jhayes82687 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...