/    Sign up×
Community /Pin to ProfileBookmark

Looping Calculation

Hi there, I have the following function:

[code]
function calculate(cnum){
price=parseFloat(document.frm1.size1.value)+parseFloat(document.frm1.base1.value);
document.frm1.price1.value=”£”+formatNumber(price);
}
[/code]

I am looking for a way to replace size1, base1 and price1 with size+cnum, base+cnum, price+cnum;

That was I can call the function calculate(1) and it will check base1, price1 and size1. Then call calculate(2) to check base2, price2 and size2.

Any ideas?

Gaz.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterFeb 07.2007 — <i>
</i>function calculate(cnum){
var price=parseFloat(document.frm1["size"+cnum].value)+parseFloat(document.frm1["base"+cnum].value);
document.frm1["price"+cnum].value="£"+formatNumber(price);
}
Copy linkTweet thisAlerts:
@CrazyGazauthorFeb 07.2007 — Awesome, exactly what I needed. I remember using it before but couldn't for the life of me remember the syntax.

Cheers,

Gaz.

A follow up question:

I have the line

<i>
</i> topping=document.frm1.toppings1[document.frm1.toppings1.selectedIndex].text;


Is it possible to convert this too?

I have tried a couple of things such as:

<i>
</i> topping=document.frm1.[["toppings"+cnum][document.frm1.toppings1.selectedIndex]].text;


EDIT: I got it fixed.

<i>
</i> topping=document.frm1(["toppings"+cnum][document.frm1.toppings1.selectedIndex]).text;
×

Success!

Help @CrazyGaz 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.18,
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,
)...