I currently have this code which calculates the total value of checkboxes when they are checked. I need to know how how I would add a radio button selection to this calculation. I need to do this as it adds a total and displays it live.
$(document).ready(function() {
function recalculate() {
var sum = 0;
var base = 0;
$("input[type=checkbox]:checked").each(function() {
sum += parseInt($(this).attr("rel"));
});
$("#output").html(sum);
}
$("input[type=checkbox]").change(function() {
recalculate();
});
});
<td width="236" height="25" align="left">Booking Period:</td>
</tr>
<tr>
<td height="10" align="right" class="align_left">One Day: ₤49 </td>
<td>
<input type="radio" name="duration" id="oneday" value="One Day Rental"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Two Day: ₤69</td>
<td>
<input type="radio" name="duration" id="two" value="Two Day Rental"/>
</td>
</tr>
<tr>
<td height="30" align="right" class="align_right">Weekend: ₤79</td>
<td>
<input type="radio" name="duration" id="weekend" value="Weekend Rental"/>
</td>
</tr>
If there is only one radio button group on this page you can simply use
:radio[name='duration']:checkedto get the currently selected radio button.And than to trigger the recalculate simply add the
:radio[name='duration']selector:Side note, you should include the radix in
parseInt(, 10)to be on the safe side.Example on jsfiddle