Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Without Javascript, no - Definately not.</p> <p>Parsing the text inside the span next to each radio using Javascript, yes - but quite tricky</p> <p>Adding an extra attribute to the radio containing the raw amount, use Javascript, yes - not very hard</p> <p>Adding an extra attribute (preferably <code>data-amount</code>), get using a library such as jQuery, yes - easy peasy lemon squeezy!</p> <hr> <p>An example for you using jQuery.</p> <p>Augmenting each of your radio's like:</p> <pre><code>&lt;input class="radio" type="radio" name="keystage1yr1" value="Paper" data-price="25" /&gt; &lt;span&gt;&amp;#163;25&lt;/span&gt; </code></pre> <p>Add a total field like:</p> <pre><code>&lt;input id="total" type="text"&gt; </code></pre> <p>And use the following code.</p> <pre><code>var values = {}; // index to store the selection from each named group of radio's $('input:radio').change(function(){ var name = $(this).attr('name'); values[name] = parseFloat($(this).data('price')); var total = 0.0; $.each(values,function(i,e){ total += e; }); $('#total').val(total.toFixed(2)); }); </code></pre> <p>Live example: <a href="http://jsfiddle.net/UL2JP/" rel="nofollow">http://jsfiddle.net/UL2JP/</a></p> <hr> <p><strong>Edit</strong>, the code above does only add up those checkboxes which are checked, unfortunately, it caches what has previously been selected in the javascript object <code>values</code>. If you, through code, unselect some checkboxes, you need to re-calculate what is selected.</p> <p>An update to this code appears to do what you describe in your comments:</p> <pre><code>var values = {}; $('input[name="keystage1save"]').click(function(){ $('input:radio:not([name="keystage1save"])').prop('checked',false); values = {}; }); $('input:radio').change(function(){ var name = $(this).attr('name'); values[name] = parseFloat($(this).data('price')); var total = 0.0; $.each(values,function(i,e){ total += e; }); $('#total').val(total.toFixed(2)); }); </code></pre> <p>Live example: <a href="http://jsfiddle.net/UL2JP/1/" rel="nofollow">http://jsfiddle.net/UL2JP/1/</a></p> <hr> <p><strong>Edit 2</strong>: Just a thought, there is no need really to cache the selected values, it would be faster to just calculate on the fly:</p> <pre><code>$('input[name="keystage1save"]').click(function(){ $('input:radio:not([name="keystage1save"])').prop('checked',false); }); $('input:radio').change(function(){ var total = 0.0; $('input:radio:checked').each(function(){ total += parseFloat($(this).data('price')); }); $('#total').val(total.toFixed(2)); }); </code></pre> <p>Live example: <a href="http://jsfiddle.net/UL2JP/2/" rel="nofollow">http://jsfiddle.net/UL2JP/2/</a></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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