/    Sign up×
Community /Pin to ProfileBookmark

Help! Auto-sum not adding up radio buttons!

hi Guys and girls,

I’ve really hit a wall with this one! My auto-sum order form won’t add together different groups of radio buttons!

I’ve been tearing my hair out all day with this one!

You can see my test form at [url]http://www.joegilbertphotography.co.uk/album/alacarte.htm[/url]

and my js at [url]http://www.joegilbertphotography.co.uk/album/test.js[/url]

Thanks, this is driving me nuts!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@nap0leonMar 21.2012 — Unless you are married to that approach (keeping a running tally, subtracting previous and adding new), why not just do a total recalculation of the form?

Here is a new calculateTotal function and a helper function "getCheckedValue" for getting the value from the radion buttons
<i>
</i>function getCheckedValue(radioName) {
var radioObj = document.getElementsByName(radioName);
if(!radioObj)
return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
if(radioObj.checked)
return radioObj.value;
else
return "";
for(var i = 0; i &lt; radioLength; i++) {
if(radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
}

function calculateTotal(){
val1 = getCheckedValue('radiobutton');
(val1.length==0) ? val1=0 : val1=val1;
val2 = getCheckedValue('radiobutton2');
(val2.length==0) ? val2=0 : val2=val2;
sum = parseFloat(val1) + parseFloat(val2);
document.getElementById('total').value = '$' + sum.toFixed(2)
}


The only change in your HTML is to change all of your
<i>
</i>onclick="this.form.total.value=calculateTotal(this);"

to
<i>
</i>onclick="calculateTotal();"


You can also remove these:
<i>
</i> &lt;input name="calculatedTotal" value="0" type="hidden"&gt;
&lt;input name="previouslySelectedRadioButton" value="0" type="hidden"&gt;


The "getCheckedValue" function also works for checkboxes.
Copy linkTweet thisAlerts:
@nap0leonMar 21.2012 — Here is a sample of the test page with a checkbox added.
<i>
</i>&lt;script type="text/javascript"&gt;
function getCheckedValue(radioName) {
var radioObj = document.getElementsByName(radioName);
if(!radioObj)
return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
if(radioObj.checked)
return radioObj.value;
else
return "";
for(var i = 0; i &lt; radioLength; i++) {
if(radioObj[i].checked) {
return radioObj[i].value;
}
}
return "";
}

function calculateTotal(){
val1 = getCheckedValue('radiobutton');
(val1.length==0) ? val1=0 : val1=val1;
val2 = getCheckedValue('radiobutton2');
(val2.length==0) ? val2=0 : val2=val2;
val3 = getCheckedValue('add5');
(val3.length==0) ? val3=0 : val3=val3

<i> </i>sum = parseFloat(val1) + parseFloat(val2) + parseFloat(val3);
<i> </i>document.getElementById('total').value = '$' + sum.toFixed(2)
}
&lt;/script&gt;
&lt;table&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td width="823"&gt;&lt;form method="POST" name="selectionForm" class="style13"&gt;
&lt;p class="style17" align="left"&gt;Select hours of coverage:&lt;/p&gt;
&lt;p class="style3 style19"&gt;
&lt;input name="radiobutton" onclick="calculateTotal();" value="1" type="radio"&gt;
1 &lt;/p&gt;
&lt;p class="style3 style19"&gt;
&lt;input name="radiobutton" onclick="calculateTotal();" value="2" type="radio"&gt;
2 &lt;/p&gt;
&lt;p class="style3 style19"&gt;
&lt;input name="radiobutton" onclick="calculateTotal();" value="3" type="radio"&gt;
3 &lt;/p&gt;
&lt;p class="style3 style19"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="style3 style19"&gt;...&lt;/p&gt;
&lt;p class="style3 style19"&gt;
&lt;input name="radiobutton2" onclick="calculateTotal();" value="1" type="radio"&gt;
1 &lt;/p&gt;
&lt;p class="style3 style19"&gt;
&lt;input name="radiobutton2" onclick="calculateTotal();" value="2" type="radio"&gt;
2
&lt;/p&gt;
&lt;p class="style3 style19"&gt;
&lt;input name="radiobutton2" onclick="calculateTotal();" value="3" type="radio"&gt;
3&lt;br&gt;
&lt;input type="checkbox" name="add5" value="5" onclick="calculateTotal();"&gt;Add $5
&lt;br&gt;
&lt;br&gt;
&lt;span class="style38"&gt;Your total is:&lt;/span&gt;
&lt;input value="0.00" name="total" id="total" readonly="readonly" onfocus="this.blur();" type="text"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;input name="button" onclick="window.print()" value="Print" type="button"&gt;
&lt;/p&gt;
&lt;p align="center"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/form&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;

×

Success!

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