forms - Javascript decimal rounding & global variable trouble -


im building checkout form. following code sets quantity form select box , bottom 1 gives me basic sub total , tax , total. works fine. though i’m sure not best code.

<!------quantity calculations & realtime update code---------->     function calquantity(){     var units=0;      var paymentform = document.forms["payment_form_id"];      var v_quantity = paymentform.elements["quantity"];      <!--need change if price changes -->      units = v_quantity.value*19.99;      return units; }  <!------subtotal - tax - total calculations & realtime update code----->         function calsubtotal(){     var subt = calquantity();     var sub_obj = document.getelementbyid('sub_id');       sub_obj.innerhtml = subt;      var tax = document.getelementbyid('tax_id');      var tax = tax.innerhtml = subt * 0.06;      var total = document.getelementbyid('total_id');      var total = total.innerhtml = subt + tax;       document.write (subt);      document.write (tax);      document.write (total);  } 

two problems:

1st need make 3 vars (subt,tax,total) round decimal 2 places i.e. ($5.56) – have seen many ways this, involved, can’t seem incorporate code. thinking of simple way have of them run through single function , output correctly.

2nd problem – in line: var tax = tax.innerhtml = subt * 0.06;

i need 0.06 (tax rate) added form. trying global var, thought simple, isn’t working. it’s select ele chooses state. value holds proper sales tax state. there form submitted before final payment info form. can generate , put proper value var in form. can’t local var on these functions. thanks


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -