/    Sign up×
Community /Pin to ProfileBookmark

How to name a radio button for auto-total?

Hey peeps!

Ok so I’m really stuck on this one. Basically I have a form which totals up all of the selections using the JS onclick=”UpdateCost()”>, and then emails it all over to me.

The problem I’m having is with radio buttons! The value needs to be numerical in order for the total to be calculated, but then I don’t know which option has been ticked.

The code is <input name=”car-colour” type=”radio” id=’game18′ value=”60.00″ onclick=”UpdateCost()”>

If I have two radio buttons in the same group with the same value, I won’t know which one’s been selected.

Please help!!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 22.2012 — Look at the 'id' to make the decision on which button has been clicked.

All 'id' values MUST be unique.

In addition, there are other ways to decide which RBtn has been clicked.
Copy linkTweet thisAlerts:
@nap0leonMar 23.2012 — I have a demo on my site that you uses the function below get the value of the radio button that was selected. It also works for checkboxes:

http://jasondahlin.com/2011/coding-tips/radioButton_validation.asp

<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 "";
}


For your example, you would call "getCheckedValue('car-colour')" and it will give you the value of the radio with that name that is selected (or blank if none are selected).

The demo page also shows how to do this using jQuery.
Copy linkTweet thisAlerts:
@lucky118authorMar 26.2012 — Thanks for your reply guys. JMRKER - the email which is sent to me doesn't show the radio button id, it just comes up as

RADIO-NAME: 60.00

Nap0lean - any idea how I can adjust my script to work like that? It's:

<script type="text/javascript">

function UpdateCost() {

var sum = 0;

var gn, elem;

for (i=0; i<11; i++) {

gn = 'game'+i;

elem = document.getElementById(gn);

if (elem.checked == true) { sum += Number(elem.value); }

}

document.getElementById('totalcost').value = sum.toFixed(2);

}

</script>
Copy linkTweet thisAlerts:
@JMRKERMar 26.2012 — Thanks for your reply guys. JMRKER - the email which is sent to me doesn't show the radio button id, it just comes up as

RADIO-NAME: 60.00

...[/QUOTE]


Are you saying that the email contains the information, not your script?

If you are trying to determine which button is selected via an email message,

then the email MUST contain more information and you will not be able to

decipher it from anything you place in your script. :eek:

It is the email supplied information that must be changed, not your script.
Copy linkTweet thisAlerts:
@lucky118authorMar 27.2012 — Lol, I don't suppose you've any idea how? My emailer can be seen at http://www.text-upload.com/read.php?id=320563&c=5925706

Thankyou x
Copy linkTweet thisAlerts:
@JMRKERMar 27.2012 — I know very little of PHP, so you might try that section of this forum.

I suspect it is in the building of the message that is sent via the email.

You might be able to change the contents of the message to identify the particular products

when the prices are duplicated.

You might be able to contact your ISP to help you with this.

Good Luck!

?
×

Success!

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