Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding values of radio buttons together
    primarykey
    data
    text
    <p>I have a form where people can build up an order choosing different options, i.e. they choose a base price first and then add to that. The radio buttons are hidden and the labels styled to be the clickable area instead.</p> <p>The requirements include being able to deselect the radio button (i.e. you can reset your choice back to null if you want) which I've got working OK in the code below.</p> <p>I also have to write the values to other elements on the page. That's working OK too.</p> <p>What's not working right is adding the values of different groups of radio buttons together.</p> <pre><code>$('.order-option + label').click(function(e) { var radio = $(this).attr('for'); radio = $('#' +radio ); var spec_price = 0; var env_price = 0; switch($(radio).attr('name')) { case 'specifications': spec_price = parseInt(radio.val()); break; case 'envelopes': env_price = parseInt(radio.val()); break; } var total_price = +spec_price + env_price; var desc = radio.data('title'); if(radio.is(':checked')) { e.preventDefault(); radio.removeAttr('checked'); } $('.order-preview-total').html('$'+total_price+' (inc gst)'); $('#order-preview-total').val('$'+total_price+' (inc gst)'); $('.order-preview-content').html(desc); $('#order-preview-content').val(desc); }); </code></pre> <p>When I click on the second row of radio buttons, it just overwrites the price, rather than adding to it.</p> <p>It's also not resetting the total to 0 if all the radio buttons are deselected again.</p> <p>The elements with classnames (e.g. .order-preview-total) are the ones displaying the info, the ones with IDs are hidden form elements for passing the data to another form.</p> <h2>Edit</h2> <p>As requested, I've created a <a href="http://jsfiddle.net/8a5FK/" rel="nofollow">JSFiddle</a>. If you click any of the buttons in the second row, the value should be added to the ones you've already selected in the first row. If you click on an option you've already chosen again, i.e. to deselect it, that value should be subtracted again.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload