Reputation: 211
Does anyone know how to sum the total of the values. And I would like to multiply the total with 1072. I tried it a littlebit. But that didn't work out very well..
So I would like a javascript code where the total of the 4 values that are marked will be calculated.
<html>
<head>
<h2>Inboedelwaardemeter</h2>
<script type="text/javascript">
function sumCheckedRadioButtons() {
var sum = 0;
$('input[type=radio]:checked').each(function(i, el) {
sum += Number($(el).val());
});
return sum;
}
$('#show_sum').on('click', function() {
alert(sumCheckedRadioButtons());
});
</script>
</head>
<body>
<br>
<b> Leeftijd hoofdkostwinner:</b>
<form name="mijnForm1">
<br>
<input type="radio" id="getal1" name="leeftijd"
value="22" checked> 35 jaar en jonger
<br>
<input type="radio" id="getal2" name="leeftijd"
value="29"> 36 t/m 50 jaar
<br>
<input type="radio" id="getal3" name="leeftijd"
value="38"> 51 jaar en ouder
<br>
<br>
<b> Samenstelling huishouden:</b>
<br>
<form name="mijnForm2">
<input type="radio" id="getal4" name="huishouden"
value="22" checked> Alleenstaande
<br>
<input type="radio" id="getal5" name="huishouden"
value="29"> Echtpaar / Samenwonende
<br>
<br>
<b> Netto maandinkomen hoofdkostwinner</b>
<br>
<form name="mijnForm3">
<input type="radio" id="getal6" name="hoofdkostwinner"
value="22" checked> Tot en met €1000,-
<br>
<input type="radio" id="getal7" name="hoofdkostwinner"
value="29"> €1001,- tot en met €2000,-
<br>
<input type="radio" id="getal8" name="hoofdkostwinner"
value="38"> €2001,- tot en met €3000,-
<br>
<input type="radio" id="getal9" name="hoofdkostwinner"
value="38"> €3001,- of hoger
<br>
<br>
<b> Oppervlakte woning</b>
<br>
<form name="mijnForm4">
<input type="radio" id="getal10" name="Oppervlakte"
value="22" checked> tot en met90m²
<br>
<input type="radio" id="getal1" name="Oppervlakte"
value="29"> 91m² tot en met 140m²
<br>
<input type="radio" id="getal12" name="Oppervlakte"
value="38"> 141m² tot en met 190m²
<br>
<input type="radio" id="getal13" name="Oppervlakte"
value="38"> 191m² of meer
<br>
</form>
<input type="button" id="show_sum" value="Show Sum" />
</body>
</html>
Upvotes: 0
Views: 531
Reputation: 4798
The following is a vanilla js solution:
function checkTotal() {
var a = document.querySelectorAll('input:checked');
var total = 0;
for(var x=0; x < a.length;x++){
total += a[x].value * 1;
}
alert(total);
}
Note: queryselector is available in (relatively) modern browsers only http://caniuse.com/queryselector
Example here: http://jsfiddle.net/2eU4E/1/
Upvotes: 1
Reputation: 156374
Using jQuery you can simply select all radio buttons which are checked and add their numerical value to a sum:
function sumCheckedRadioButtons() {
var sum = 0;
$('input[type=radio]:checked').each(function(i, el) {
sum += Number($(el).val());
});
return sum;
}
Here is a working jsFiddle (press the "Show Sum" button at the bottom of the "Result" pane).
Upvotes: 2