Jack Johnson
Jack Johnson

Reputation: 611

Issues with changing dynamic prices

I have a car rental company that offers pre-paid discount of 10%. I have formatted the total price correctly, but when someone adds an "Extra" to it, it stops changing the price total with the 10%.

This is the price field:

<span id="cashamount" class="additional xxlarge carrental_security_deposit" data-deposit="<?php echo $available_payments['carrental-paypal-security-deposit'];?>" data-deposit-round="<?php echo $available_payments['carrental-paypal-security-deposit-round'];?>"> - </span> <span class="additional xxlarge">ISK</span>

And this is the Javascript i use:

<script type="text/javascript">//<![CDATA[
window.onload=function(){
Number.prototype.formatMoney = function(c, d, t){
var n = this, 
    c = isNaN(c = Math.abs(c)) ? 2 : c, 
    d = d == undefined ? "." : d, 
    t = t == undefined ? "," : t, 
    s = n < 0 ? "-" : "", 
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
    j = (j = i.length) > 3 ? j % 3 : 0;
   return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 };

var Total = $("#cashamount");
var totalNumber = Number(Total.text().replace(/[^0-9\.]+/g,""));
Total.text((totalNumber * 1.1).formatMoney(2, '.', ','));



}//]]> 

</script>

It's there onChange variable or something that monitors the changes to the price field and changes accordingly ?.

Any help on this is greatly appreciated.

Upvotes: 0

Views: 38

Answers (1)

bwegs
bwegs

Reputation: 3767

You can use jquery to execute a function every time a specific event is fired, in your case something like the jquery change function should do the trick:

$('#idOfPriceField').change(function() {
    // make the desired updates here
});

Upvotes: 1

Related Questions