Skip to content Skip to sidebar Skip to footer

Javascript Calculate Values Based On Checked Options

I was tasked to edit an existing Joomla code.This a javascript file that will calculate value depending on which options is checked. file.html

Solution 1:

  1. Your switch statement needs to have "break;" after each "case".
  2. This was always returning -1 (not found) no matter what was selected:

    var current_index = cat_buttons.index(cat_buttons.find(':checked'))

Here is a the working code:

<scripttype="text/javascript"src="https://code.jquery.com/jquery-2.2.0.min.js"></script><h2class="contentheading"style="margin-top: 0px;">2016 “Train-The-Trainer” Workshops<br /><br /> AERIAL</h2><div><p>$50.00 per class/person<br />Sign up for both for $90.00</p></div><div><formid="paypal_submit_form"action="https://www.paypal.com/***"method="post"><inputname="cmd"type="hidden"value="_cart" /><inputname="upload"type="hidden"value="1" /><inputname="charset"type="hidden"value="utf8" /><inputname="business"type="hidden" /><inputname="currency_code"type="hidden"value="USD" /><inputname="custom"type="hidden" /><inputname="amount"type="hidden" /><inputname="first_name"type="hidden" /><inputname="last_name"type="hidden" /><inputname="address1"type="hidden" /><inputname="city"type="hidden" /><inputname="state"type="hidden" /><inputname="zip"type="hidden" /><inputname="email"type="hidden" /><inputname="night_phone_b"type="hidden" /><inputname="address_override"type="hidden"value="1" /><divid="paypal_prs"style="font-size: 12px;"><p><inputid="cat-both"checked="checked"name="cat"type="radio"value="90" /><labelfor="cat-both">Both</label><inputid="cat-aerial"name="cat"type="radio"value="50" /><labelfor="cat-aerial">Aerial</label><inputid="cat-loto"name="cat"type="radio"value="50" /><labelfor="cat-loto">Lockout/Tagout</label></p><br /> Members:
            <selectid="bal_number_of_members"style="font-size: 12px; padding: 3px;"name="number_of_members"><optionvalue="1">1</option><optionvalue="2">2</option><optionvalue="3">3</option><optionvalue="4">4</option><optionvalue="5">5</option><optionvalue="6">6</option><optionvalue="7">7</option><optionvalue="8">8</option><optionvalue="9">9</option><optionvalue="10">10</option><optionvalue="11">11</option></select><br /> Total Amount (Cost + Processing Fee): 
                <inputstyle="width: 50px; margin-right: 10px; padding: 2px; padding-bottom: 1px; font-size: 12px;"name="tmp_total_amount"readonly="readonly"type="text"value="93" /><inputid="bal_submit_btn"style="padding: 3px 5px; font-size: 12px; cursor: pointer;"type="button"value="Pay Here" /><inputname="item_name_1"type="hidden"value="2016 Train-The-Trainer Workshops(AERIAL LIFTS &amp; LOCKOUT/TAGOUT)" /><inputname="amount_1"type="hidden"value="90" /><inputname="quantity_1"type="hidden"value="1" /><inputname="item_name_2"type="hidden"value="Processing fee" /><inputname="amount_2"type="hidden"value="0" /><inputname="quantity_2"type="hidden"value="1" /></div><inputname="notify_url"type="hidden"value="http://some.org/tmp_ipn.php" /><inputname="return"type="hidden"value="http://some.org/" /><inputname="cancel_return"type="hidden"value="http://some.org/index.php?view=article&amp;id=278" /><inputname="no_shipping"type="hidden"value="1" /></form></div><divstyle="font-size: 11px; margin-top: 10px; color: red;">
    Additional 2.9% + $0.30 processing fee will be charged with all orders paid by credit card.
</div><divstyle="margin-top: 50px;"><astyle="font-size: 18px;"href="images/Flyer_2016.pdf"target="_blank">Download Order Form</a></div><pstyle="font-size: 14px;"><strong>Please fax or email the order form to the office.</strong></p><script>functiongetPayPalProcessingFee() {
         var qty = jQuery('#paypal_submit_form select[name=number_of_members]').val();
         qty = parseInt(qty);

         //var cat_buttons = jQuery("input[name=cat]")//var current_index = cat_buttons.index(cat_buttons.find(':checked'));var current_index = $("input[name=cat]:checked").attr('id');

         switch (current_index){
             case'cat-both':
                return3*qty;
                break;
             case'cat-aerial':
                return0;
                break;
             case'cat-loto':
                return1.8*qty;
                break;
             default:
                return0;
         }
     }

    functiontrainthetrainerForm_calculateItemAmount() {
        var qty = jQuery('#paypal_submit_form select[name=number_of_members]').val();
        var current_val = jQuery("input[name=cat]:checked").val();

        var amount = parseInt(qty) * current_val;
        amount = parseFloat(amount).toFixed(2);
        return amount;
    }

    functiontrainthetrainerForm_calculateFee() {
        var fee = getPayPalProcessingFee();
        return fee;
    }

    functiontrainthetrainerForm_displayTotalAmount() {
        var amount = trainthetrainerForm_calculateItemAmount();
console.log(amount);        
        var fee = getPayPalProcessingFee();
console.log(fee);        
        var totalamount = parseFloat(amount) + parseFloat(fee);
console.log(totalamount);        
        totalamount = parseFloat(totalamount).toFixed(2);
console.log(totalamount);

        jQuery('#paypal_submit_form input[name=tmp_total_amount]').val(totalamount);
    }

    functionsubmitTrainthetrainerForm() {
        var qty = jQuery('#paypal_submit_form select[name=number_of_members]').val();
        jQuery('#paypal_submit_form input[name=quantity_1]').val(qty);

        var totalAmount = 0;
        var amount = trainthetrainerForm_calculateItemAmount();
        var processingFee = trainthetrainerForm_calculateFee();

        totalAmount = amount + processingFee;

        jQuery('#paypal_submit_form input[name=business]').val('shana@same.org');
        jQuery('#paypal_submit_form input[name=amount]').val(totalAmount);
        jQuery('#paypal_submit_form input[name=amount_2]').val(processingFee);
        jQuery('#paypal_submit_form').submit();
        returntrue;
    }

    jQuery(document).ready(function (){
        $(document).on('change',$("#bal_number_of_members"),function(){
            trainthetrainerForm_displayTotalAmount();
        });
        jQuery("#bal_submit_btn").click(function() {
            submitTrainthetrainerForm();
        });

        trainthetrainerForm_displayTotalAmount();
    });
</script>

Post a Comment for "Javascript Calculate Values Based On Checked Options"