Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your javascript is executed before the HTML is generated, so it doesn't "see" the ungenerated INPUT elements. For jQuery, you would either stick the Javascript at the end of the HTML or wrap it like this:</p> <pre><code>&lt;script type="text/javascript"&gt; $(function() { //jQuery trick to say after all the HTML is parsed. $("input[type=radio]").click(function() { var total = 0; $("input[type=radio]:checked").each(function() { total += parseFloat($(this).val()); }); $("#totalSum").val(total); }); }); &lt;/script&gt; </code></pre> <p><em>EDIT: This code works for me</em></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;/head&gt; &lt;body&gt; &lt;strong&gt;Choose a base package:&lt;/strong&gt; &lt;input id="item_0" type="radio" name="pkg" value="1942" /&gt;Base Package 1 - $1942 &lt;input id="item_1" type="radio" name="pkg" value="2313" /&gt;Base Package 2 - $2313 &lt;input id="item_2" type="radio" name="pkg" value="2829" /&gt;Base Package 3 - $2829 &lt;strong&gt;Choose an add on:&lt;/strong&gt; &lt;input id="item_10" type="radio" name="ext" value="0" /&gt;No add-on - +$0 &lt;input id="item_12" type="radio" name="ext" value="2146" /&gt;Add-on 1 - (+$2146) &lt;input id="item_13" type="radio" name="ext" value="2455" /&gt;Add-on 2 - (+$2455) &lt;input id="item_14" type="radio" name="ext" value="2764" /&gt;Add-on 3 - (+$2764) &lt;input id="item_15" type="radio" name="ext" value="3073" /&gt;Add-on 4 - (+$3073) &lt;input id="item_16" type="radio" name="ext" value="3382" /&gt;Add-on 5 - (+$3382) &lt;input id="item_17" type="radio" name="ext" value="3691" /&gt;Add-on 6 - (+$3691) &lt;strong&gt;Your total is:&lt;/strong&gt; &lt;input id="totalSum" type="text" name="totalSum" readonly="readonly" size="5" value="" /&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $("input[type=radio]").click(function() { var total = 0; $("input[type=radio]:checked").each(function() { total += parseFloat($(this).val()); }); $("#totalSum").val(total); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

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