/    Sign up×
Community /Pin to ProfileBookmark

I am new to javascript and seem to have stumped myself on this one.
I am tying to calculate a total from 6 form fields named life1, life2 …

basically the expression would be something like:
life1 + life2 + life3 + life4 + life5 – life6 = Total

I have been working on this for sometime now as a side project and can’t seem to figure this out.
I know that i need to convert the string using parseInt or similar function but I am honestly not sure how to get that working either.

any help would be greatly appreciated.
Thanks in advance.
Josh

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@AdamGundryJun 11.2004 — The basic syntax for accessing a form field looks like this:

document.[i]formname[/i].[i]fieldname[/i].value

So you can probably do something like this:

total = parseInt(document.form.life1.value) + parseInt(document.form.life1.value) + parseInt(document.form.life1.value) + parseInt(document.form.life1.value) + parseInt(document.form.life1.value) - parseInt(document.form.life1.value);

Adam
Copy linkTweet thisAlerts:
@CombustionauthorJun 11.2004 — Thanks I got it working now.

For anyone else who is interested the final code I used for the function was:


<!--

function calculateTotal(){

total = parseInt(document.frmLifeNeed.life1.value) + parseInt(document.frmLifeNeed.life2.value) + parseInt(document.frmLifeNeed.life3.value) + parseInt(document.frmLifeNeed.life4.value) + parseInt(document.frmLifeNeed.life5.value) - parseInt(document.frmLifeNeed.life6.value);

alert(total);

}

//-->
Copy linkTweet thisAlerts:
@CombustionauthorJun 11.2004 — Thanks I got it working now.

For anyone else who is interested the final code I used for the function was:


<!--

function calculateTotal(){

total = parseInt(document.frmLifeNeed.life1.value) + parseInt(document.frmLifeNeed.life2.value) + parseInt(document.frmLifeNeed.life3.value) + parseInt(document.frmLifeNeed.life4.value) + parseInt(document.frmLifeNeed.life5.value) - parseInt(document.frmLifeNeed.life6.value);

alert(total);

}

//-->
Copy linkTweet thisAlerts:
@crh3675Jun 11.2004 — re-write:

<i>
</i>function calculateTotal(){
var fields=Array("life1","life2","life3","life4","life5","life6");
for(i=0;i&lt;fields.length;i++){
str="var val"+i+"=parseInt(document.frmLifeNeed."+fields[i]+".value);";
eval(str);
}
total=val0+val1+val2+val3+val4-val5
alert(total)
}
Copy linkTweet thisAlerts:
@CombustionauthorJun 11.2004 — Ya, that does seem a bit more efficient?
Copy linkTweet thisAlerts:
@neil9999Jun 11.2004 — Instead of:

parseInt(document.frmLifeNeed.life1.value)

You can use this:

document.frmLifeNeed.life1.value*1

Timesing it by one makes it into a number.

Neil
×

Success!

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

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

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