1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Taxes based on State

Discussion in 'JavaScript' started by goneinsane, Sep 10, 2019.

  1. #1
    Hello,

    I have a form with a dropdown menu with an id of #state. I would like to make an IF statement that says that if the selected state was NY, var tax5 = 1.05;. Everything else will be var tax15 = 1.15;. How can I make this happen? Below is the code.

            
            var tax15 = 1.15;
            var tax5 = 1.05;
    
            var qty = $("#qty").val();
            var price = parseFloat(data.replace("$", ""));
            var shipping = parseFloat(data2.replace("$", ""));
            if(!shipping){ shipping = 0; }
            if($('#store_pickup').prop("checked") == true){
              var shipping = 0;
                   console.log('check');         
                }
            $("#shippinginput").val(shipping.toFixed(2));
            $("#subtotal").html("$"+(price*qty).toFixed(2));
            $("#shipping").html("$"+(shipping*qty).toFixed(2));
            $("#tax").html("$"+(((price+shipping)*qty)*tax5).toFixed(2));
            $("#total2").html("$"+((price+shipping)*qty).toFixed(2));
            $("#cardSubmitBtn").val('BUY NOW $'+((price+shipping)*qty).toFixed(2));
    Code (JavaScript):

    SEMrush
     
    goneinsane, Sep 10, 2019 IP
    SEMrush
  2. hdewantara

    hdewantara Active Member

    Messages:
    437
    Likes Received:
    37
    Best Answers:
    19
    Trophy Points:
    80
    #2
    Something like below....
    <select id="state">
      <option value="AL">alabama</option>
      <option value="NY">new york</option>
      <option value="CO">colorado</option>
    </select>
    HTML:
    in pure javascript though:
    <script>
          var tax = 1.15;
          document.getElementById('state').addEventListener('change',function(){
          if(this.value === 'NY')
            tax = 1.05;
        else
            tax = 1.15;
        },false);
    
    var qty = $("#qty").val();
    var price = parseFloat(data.replace("$", ""));
    var shipping = parseFloat(data2.replace("$", ""));
    if(!shipping){ shipping = 0; }
    if($('#store_pickup').prop("checked") == true){
    var shipping = 0;
    console.log('check');
    }
    $("#shippinginput").val(shipping.toFixed(2));
    $("#subtotal").html("$"+(price*qty).toFixed(2));
    $("#shipping").html("$"+(shipping*qty).toFixed(2));
    $("#tax").html("$"+(((price+shipping)*qty)*tax).toFixed(2));
    $("#total2").html("$"+((price+shipping)*qty).toFixed(2));
    $("#cardSubmitBtn").val('BUY NOW $'+((price+shipping)*qty).toFixed(2));
    </script>
    
    Code (JavaScript):
     
    hdewantara, Sep 10, 2019 IP
    goneinsane likes this.
  3. goneinsane

    goneinsane Active Member

    Messages:
    298
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #3
    After removing the first occurrence of var tax = 1.15, it worked. Thank you so much.
    Do you know how I could include 2 states without repeating? For example, if NY or PA are picked?
     
    Last edited: Sep 11, 2019
    goneinsane, Sep 11, 2019 IP
  4. hdewantara

    hdewantara Active Member

    Messages:
    437
    Likes Received:
    37
    Best Answers:
    19
    Trophy Points:
    80
    #4
    if(this.value === 'NY' || this.value === 'PA' || ...)
         tax = 1.05;
              else
         tax = 1.15;
    Code (JavaScript):
    should be okay I think -- what's repeating?

    Perhaps to use switch statement?
    
    switch(this.value){
         'NY': 
         'PA':
              tax = 1.05;
              break;
         default:
              tax = 1.15;
    }
    
    Code (JavaScript):
     
    Last edited: Sep 11, 2019
    hdewantara, Sep 11, 2019 IP
  5. goneinsane

    goneinsane Active Member

    Messages:
    298
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #5
    That worked. Thanks.
     
    goneinsane, Sep 11, 2019 IP
  6. hdewantara

    hdewantara Active Member

    Messages:
    437
    Likes Received:
    37
    Best Answers:
    19
    Trophy Points:
    80
    #6
    am happy to help you :)
     
    hdewantara, Sep 11, 2019 IP