Search code examples
javascriptjqueryselectcalc

Update price on change inlcuding on select


This is my fiddle: https://jsfiddle.net/btekbtek/6gxztk4f/6/

When I type qty, I automatically calculate price.
I want also include select option that is adding
£1 per 1 qty.

If someone type 10 qty - price should be qty10*(£1*10)*price

Currently when I add:

 // update price if option change
 var optionprice = 0;
 var getPriceOption = function() {
   jQuery("#select_21").change(function() {
     if (jQuery(this).val() === '63') {
       optionprice = 0;
     } else {
       optionprice = 1;
     }
   }); //end update optionprice
   return optionprice;
 }; //end get PriceOption

 console.log(getPriceOption);

getPriceOption is undefined. I was trying to add it after before and same result.

I cannot change anything in HTML, just in jQuery.


Solution

  • This was enough:

    var updateprice = $("#select_21 option:selected").attr('price');
    

    added in :

    while (i--) {
             if (qty >= tierPrices[i]['price_qty']) {
               var updateprice = $("#select_21 option:selected").attr('price');
               var calc_price = tierPrices[i]['price'] * qty + (updateprice*qty);
               return (calc_price).toFixed(2)
             }
           }