[Solved] How to add tax and deals to a sales price in JavaScript


I see you’re asking a conceptual question.

I would approach this by adding in data attributes to your select drop down. Then grabbing the values with a simple function on select change and integrating that into your price equation.

You can read about data attributes here: https://www.w3schools.com/tags/att_global_data.asp and https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

1) Add data attributes

<select id="province">
  <option value="saskatchewan" data-shipping-cost="0" data-tax="0.05" data-deal-limiter="30" data-deal-coupon="5'>Saskatchewan</option>
  ...
</select>

2) Grabbing selected values (please note camelCase access pattern for data attr’s)

document.getElementById("province").addEventListener("change", function() {

    const select = document.getElementById("province"),
          selectedProvince = select.options[select.selectedIndex],
          shippingCost = selectedProvince.dataset.shippingCost ,
          tax = selectedProvince.dataset.tax,
          dealLimiter = selectedProvince.dataset.dealLimiter,
          dealCoupon = selectedProvince.dataset.dealCoupon;

});

3) Integrating into your equation

You can do this part yourself; I teach fishing, I don’t give fish. (Hint: involves an if statement).

8

solved How to add tax and deals to a sales price in JavaScript