Wednesday 15 May 2013

javascript - I have 2 price values and I want to calculate the difference -



javascript - I have 2 price values and I want to calculate the difference -

i have 2 cost range. regular cost , offer cost displayed on screen below html rendered.

i trying calculate cost difference using javascript/jquery , insert div discount price.

<div class="price-box"> <p class="old-price"> <span class="price-label">regular price:</span> <span class="price" id="old-price-221">rs17.00 </span> </p> <p class="special-price"> <span class="price-label">special price</span> <span class="price" id="product-price-221">rs14.45 </span> </p> <div> <span>discount price:</span> <span>xxxxxx</span> </div> </div>

i have currency symbol displayed along price. so, wondering how calculate difference between "regular price" , "special price".

can 1 please help me?

p.s. searched site , did not find relevant answers.

there : http://jsfiddle.net/s7w98ngt/2/

jquery part :

$('.price-box').each(function(){ var element = $(this), oldpriceraw = element.find('.old-price .price').text(), specialpriceraw = element.find('.special-price .price').text(), oldprice = parsefloat(oldpriceraw.replace('rs','')), specialprice = parsefloat(specialpriceraw.replace('rs','')), diff = 'rs'+(oldprice - specialprice).tofixed(2), // 2 numbers after comma diffelement = element.find('.diff-price'); diffelement.text(diff); });

html bit modified :

<div class="price-box"> <p class="old-price"> <span class="price-label">regular price:</span> <span class="price" id="old-price-221">rs17.00 </span> </p> <p class="special-price"> <span class="price-label">special price</span> <span class="price" id="product-price-221">rs14.45 </span> </p> <div> <span>discount price:</span> <span class="diff-price"></span> </div> </div>

the tricky thing current prices value float numbers. then, calcul difference.

javascript jquery

No comments:

Post a Comment