/    Sign up×
Community /Pin to ProfileBookmark

HTML form, js calc for separate areas… need help

Hi Gang,
I’ve been searching everywhere for some help on this, and I know it doesn’t help that I’m a total newb to js,.. but please bare with me.. any help you provide is greatly appreciated!

Here is what I have:

An HTML Form with a radio buttons and checkboxes,.. about 17 radio buttons [U]groups[/U] and 21 checkboxes in total..
right now I have two js snippets to make each of two calcs run. I would like to condense them into snippet.

[U]I only need to calculate two values though.. one value is just the value from a single group of radio buttons : radio50, a group of 3 buttons[/U]
Not all the values in the form.. ?

[B]Here is the code I have for this first calc:[/B]

[code]
function setupvalue()
{
for (i=0;i<document.form_order.radio50.length;i++)
{
if (document.form_order.radio50[i].checked)
{
radio50_value = “$” + document.form_order.radio50[i].value;
}
}
document.form_order.radio50txt.value=radio50_value;
}
[/code]

The next value I need to calc is the value for a group of 2 radio buttons: radio51 and radio52 and the 5 checkboxes below them.

The problem I run into is that the script I have for the second calc totals the value for ALL the fields.. so, all the radio buttons/checkboxes are totaled..

[B]Here is the code I have for the second calc:[/B]

[code]

function calculateTotal(from_order){
var total = 0;
var elems = from_order.getElementsByTagName(‘input’);
for(var i = 0; i < elems.length; i++){
if((elems[i].type == “radio” || elems[i].type == “checkbox”) && elems[i].checked){
total += parseFloat(elems[i].value);
}
}
total = total.toFixed(2)
from_order.total.value = “$” + total;
}
[/code]

Keep in mind that this is all in one form..

Here is a visual of what I’m trying to accomplish:

[code]
<form>
* bunch of radio buttons
* some text fields
* some check boxes

* radio button group: radio50 – I need the sole value from this group to display in a field at the bottom of the form

* radio button group: radio51 – I need the value for this line and the lines below to calc in a separate field at the bottom of the form.
* radio button group: radio 52
* 5 more checkboxes

* text field: value of radio50

* text field: value of radio51+radio52+5 checkboxes if checked.
</form>
[/code]

Any help is greatly appreciated! Thank you.. I have been learning a lot just browsing this forum,.. I’m hoping that will continue as I get ‘into’ the forum and start being an active member.

J

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangDec 17.2008 — I assume from your explanation that the first calculation works correctly?

The second case wrap the targeted fields in a div and reference them from there:<i>
</i>&lt;div id="secondgroup"&gt;
* radio button group: radio51 - I need the value for this line and the lines below to calc in a separate field at the bottom of the form.
* radio button group: radio 52
* 5 more checkboxes
&lt;/div&gt;

var elems = from_order.getElementById('secondgroup').getElementsByTagName('input');
Copy linkTweet thisAlerts:
@revivedauthorDec 17.2008 — Excellent, thank you.. I'll play around with it and see what I can do ?

Thanks for the help.
×

Success!

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