/    Sign up×
Community /Pin to ProfileBookmark

Sum radio buttons values

Hi, im new to javascript and i need to develop a test in javascript and html (90 questions and each question has 3 radio buttons for the 3 posible answers) , the html part is done, in the javascript part i need to get in 6 different variables the value of some radio buttons . depending of the question each question add its radio button value to a any of the 6 variables , so in my html code i grouped the question by names and each one has a different id, so the javascript can access them as an array. The problem is that when i use alert to show the results of 2 different questions, for example 2 and the other 2, the alert is showing me 2 2 instead of 4. Here is my code:

document.querySelector(‘.button’).addEventListener(‘click’, function(){

//var totalI
var totalI =0;

function pregunta1(totalI){
for (var i=0; i<document.form.pregunta1.length; i++) {
if (document.form.pregunta1[i].checked) {

totalI = document.form.pregunta1[i].value;
return totalI;

}
}

}

function pregunta4(totalI){
for (var i=0; i<document.form.pregunta4.length; i++) {
if (document.form.pregunta4[i].checked) {

totalI = document.form.pregunta4[i].value;
return totalI;

}
}

}

totalI = (pregunta1(totalI) + pregunta4(totalI));
alert(totalI);

//alert(totalR + ” ” + totalC + ” ” + totalI + ” ” + totalL + ” ” + totalB + ” ” + totalS);

});

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumDec 13.2018 — Regarding your question: The value of your radio buttons is of type string. Adding two strings by use of the + sign will not provide the sum but will concatenate the strings instead. You need to use the function parseInt to convert the strings to numbers before adding them.

I recommend to simplify your code a bit: Using the function document.querySelector and an appropriate selector will get the value of the radio button that is checked in one line without a loop:
function pregunta(sel) {
return parseInt(document.querySelector(sel).value);
}
var total1 = pregunta("[name='pregunta1']:checked");
var total2 = pregunta("[name='pregunta2']:checked");
var total3 = pregunta("[name='pregunta3']:checked");
var totalAll = total1 + total2 + total3;
Copy linkTweet thisAlerts:
@wbportDec 13.2018 — I've used the following to read sets of radio buttons: function readRadio(i) {
var sel = document.getElementsByName("S"+(6-i));
for (var i=0; i&lt;sel.length; i++) {
if (sel[i].checked == true) return i;
}
}
You might need to add a final 'return -1' if there is a possibility none of the buttons is pushed. It's from a page that creates guitar chords: https://www.wjporter.com/nwc/guitar.htm
×

Success!

Help @JulienDeArc 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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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