Note that there are some explanatory texts on larger screens.

plurals
  1. POForm calculator using Each() and Children() in jQuery
    primarykey
    data
    text
    <p>Im trying to build a form that calculates a total price based on a series of drop down boxes with string values such as "This option costs £30" i know this is not ideal but im putting this together as a hack for an existing script</p> <p>For the most part ive got it working however im not sure how to run the each function for each child of #productconfig </p> <p>I can manually input each of the drop downs ids into an array and that makes the calculation but it would be good if it just worked with all the children of #productconfig </p> <pre><code>&lt;code&gt; &lt;div id="#productconfig"&gt; &lt;label&gt;Model Type&lt;/label&gt; &lt;select name="products[220][data][modeltype]" id="data-modeltype-220"&gt; &lt;option value="M-Type £500"&gt;M-Type £500&lt;/option&gt; &lt;option value="P-Type £500"&gt;P-Type £500&lt;/option&gt; &lt;option value="S-Type £500"&gt;S-Type £500&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;/code&gt; &lt;code&gt; $(document).ready(function() { $("#productconfig").children().change(function () { calculateoptions(); }); calculateoptions(); }); &lt;/code&gt; &lt;code&gt; function calculateoptions() { var arr = ["data-modeltype-220"]; var total = 0; jQuery.each(arr, function () { var str = $('#' + this).attr("value"); var poundsign = str.indexOf('£'); var poundsign = poundsign + 1; var lengthofstr = str.length; var shortstr = str.substr(poundsign, lengthofstr); total = eval(total) + eval(shortstr); }); $('#price').html("£" + total); } &lt;/code&gt; </code></pre>
    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