Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a javascript that creates a dynamic total depending on radio box checked in html form
    primarykey
    data
    text
    <p>Dearest stackoverflow friends,</p> <p>I am creating an online html form and have created a javascript function to dynamically (not completely sure I'm using this word correctly) display the total due in membership fees at the bottom of the form as options are selected. The total fee depends on one's membership type, country, and method of payment. It worked perfectly when all I was calculating was the membership type and postage according to country (I used drop down forms for these two options). Now I'd like to add the third term to the equation (the method of payment - one has a choice of cheque or paypal) but I can't get it to work. I'm using radio buttons this time.</p> <p>My totalling function is this (without "+ getPaypalfee()" it works just fine):</p> <pre><code>function getAmountDue() { var amountDue = getMembershipPrice() + getExtraPostagePrice() + getPaypalfee(); document.getElementById('amountDue').innerHTML ="Amount Due: $"+amountDue; } </code></pre> <p>The javascript I wrote to return the paypal fee is this (it's become very convoluted and I'm not sure where I've gone wrong and how to restart!):</p> <pre><code>var paymentmethod_Fee = new Array(); paymentmethod_Fee["cheque"]=0; paymentmethod_Fee["paypal"]=2; function getPaypalfee() { var paypalFee=0; for (var i=0; i &lt; document.membershipform.payment_method.length; i++) { if (document.membershipform.payment_method[i].checked) { var selectedPaymentmethod = document.membershipform.payment_method[i].value; } } paypalFee = paymentmethod_Fee[selectedPaymentmethod.value]; return paypalFee; } </code></pre> <p>The html for the radio buttons looks like this:</p> <pre><code>&lt;p&gt;I will make payment via: &lt;BR&gt; &lt;input type="radio" id="payment_method" name="payment_method" value="cheque" checked="yes" onchange="getAmountDue()"&gt;Cheque &lt;input type="radio" id="payment_method" name="payment_method" value="paypal" onchange="getAmountDue()"&gt;Paypal (Add $2) </code></pre> <p>Any insights into the flaw in my logic is greatly appreciated! I'm a javascript novice and radio buttons seem to be my nemesis (I'd like to learn how to use them rather than replace them with a drop-down menu or something I know how to do already).</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. 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