Javascript bug in i.e function returning NAN (works in other browsers) -
hello have created javascript function recaculates grand total of order when delivery method changed. code works fine in firefox, chrome , i.e 9 returns nan in i.e 8 , 7, understand means not number.
i have tried using parseint, paresefloat , number can't understand why not work in i.e 6, 7 or 8
my code follows
<script type="text/javascript"> function changeprice(a) { // grabs delivery price of selected option i.e 10.99 var price = parsefloat(a); // grab delivery price on screen i.e 4.99 old_price = document.getelementbyid('delivery-sidebar').innerhtml; // removing span tags around function not needed old_price = old_price.replace('<span class=price>',''); old_price = old_price.replace('</span>',''); //grab subtotal price (which not include delivery) on screen i.e 34.99 var subtotal = document.getelementbyid('overall').innerhtml; // removing span tags around function not needed subtotal = subtotal.replace('<span class="price">',''); subtotal = subtotal.replace('</span>',''); subtotal = subtotal.replace('£',''); // converting subtotal float subtotal=parsefloat(subtotal); // if price of delivery not match price on screen if (price != old_price) { //add new price against subtotal var overall_total = (parsefloat(subtotal))+(parsefloat(price)); // round result 2 decimal places i.e 10.99 overall_total=roundnumber(overall_total,2); // update values on screen document.getelementbyid('delivery-sidebar').innerhtml = '<span class="price">£'+price.tofixed(2)+'</span>'; document.getelementbyid('grand-price').innerhtml = '<span class="price">£'+overall_total.tofixed(2)+'</span>'; } } function roundnumber(num, dec) { var result = math.round(num*math.pow(10,dec))/math.pow(10,dec); return result; } </script>
and markup
<ul> <li> <input name="shipping_method" value="tablerate_bestway" id="s_method_tablerate_bestway" checked="checked" class="radio validation-passed" onclick="changeprice('0')" type="radio"> <label for="s_method_tablerate_bestway">uk delivery (3 - 7 working days) </label> <span class="price">free delivery</span> </li> </ul> <dd> <ul> <li> <input name="shipping_method" value="flatrate_flatrate" id="s_method_flatrate_flatrate" class="radio validation-passed" onclick="changeprice('10')" type="radio"> <label for="s_method_flatrate_flatrate">next day - orders before 2pm </label> <span class="price"><span class="price">£10.00</span></span> </li> </ul> </dd>
you can never rely on reading same html page created with.
if read <span class="price">
in ie <span class=price>
(sans "
) replace fails , parse numeric returns nan.
you can either choose rely on .replace('<span class=price>','i')
or better give span id: <span id="totalprice" class="price">£xxx</span>
, numeric value innerhtml
, or better retain numeric values in memory in first place.
Comments
Post a Comment