I have checked the following tutorials:
How might I calculate the sum of radio button values using jQuery?
How to find a total sum of radio button values using jQuery/JavaScript?
How might I calculate the sum of radio button values using jQuery?
However, none of these tutorials helped me with my code.
I have about 100 radio buttons asking various questions. One of the radio buttons asks if the user wants to download a logo; if Yes, the price is $49.99, else it is $0.00. The user must choose either Silver or Gold plan. The price of the chosen plan has to be added to the logo price, and the total displayed.
This is what I have so far :
$(document).ready(function(){
$('input').iCheck ({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue',
increaseArea: '20%' // optional
});
var sum = 0;
$('input').on("change", function(){
sum = parseInt($('input[name="groupeight"]:checked').val(),10) + parseInt($('input[name="groupnine"]:checked').val(),10);
console.log(sum);
if($('input[name="groupeight"]').is(':checked') && $('input[name="groupnine"]').is(':checked')){
$('#output').html("$"+sum);
}
});
$(".choice").blur(function(){
$(this).parseNumber({format:"#,###.00", locale:"us"});
$(this).formatNumber({format:"#,###.00", locale:"us"});
});
$(".plan").blur(function(){
$(this).parseNumber({format:"#,###.00", locale:"us"});
$(this).formatNumber({format:"#,###.00",locale:"us"});
});
});
<div class="input-wrapper">
<div class="answer"><input class="choice" type="radio" name="groupeight" value="49.99"/>
<label>Yes</label>
</div>
<input class="choice" type="radio" name="groupeight" value="0.00" /><label>No</label><br />
</div>
<div class="plan-wrapper">
<label id="silver-plan"><input class="plan" type="radio" name="groupnine" value="699" <?php if (!isset($_POST['radio']) || isset($_POST['radio']) && $_POST['radio'] == '699'): ?>checked="checked"<?php endif; ?> /><label>Silver Plan</label><span id="silver-plan-price">$699</span></label>
<label id="gold-plan"><input class="plan" type="radio" name="groupnine" value="999" <?php if (isset($_POST['radio']) && $_POST['radio'] == '999'): ?>checked="checked"<?php endif; ?>/><label>Gold Plan</label><span id="gold-plan-price">$999</span></label>
</div>