javascript - Calculate Tax from price displayed in HTML table -


i trying display price including tax on product page. price displayed in [ourprice], need pull displayed price (eg £0.95),times 1.2 , display price in pounds. here code trying use, however, doesnt display calculated price. show £ symbol in front of number.

    <table cellspacing="1" cellpadding="0" border="0" >   <tbody><tr>     <td class="retail-price" valign="top" nowrap="">[retailpricetitle]</td>     <td width="100%">[retailprice]</td>   </tr><tr>     <td class="our-price" valign="top" nowrap="">our price:</td>     <td width="100%"><span id="op">[ourprice]</span></td>   </tr>   <tr>     <td class="incvat" valign="top" nowrap="">inc vat</td>     <td class="incvat">  <script type="text/jscript"> var ours = document.getelementbyid("op").value;   document.write (£(ours *1.2))      </script> </td>   </tr>    </tbody> </table> 

that's not syntactically valid javascript. try this:

var ourprice = document.getelementbyid("op").innerhtml; // fetch price ourprice = ourprice.replace('£', ''); // remove pound character ourprice = parsefloat(ourprice); // convert price string number var incvat = ourprice * 1.2; // calculate price tax document.write('£' + incvat); // write result 

http://jsfiddle.net/rjwmk/


however, there other issues code whole other topics in , beyond scope of answer. research exercise:

  1. don't use document.write(). instead run script find value after whole page loads, , inserts value content of correct node.

  2. format result using number.tofixed(2) or currency formatting library.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -