dynamic calculate discount from input boxes jscript

Posted by Merlin under jQuery on 9/19/2012 | Points: 10 | Views : 2165 | Status : [Member] | Replies : 2
I am very new to jscript /jquery client side programming after years of server side php and would like some help please.

I have 3 input boxes
rent_price
booking_costs
discount

<tr>
<td><input name="rent_price[]" type="text" class="txtBox" id="rent_price[]" ?>" size="5" /> &nbsp;</td>
<td><input name="booking_costs[]" type="text" class="txtBox" id="booking_costs[]" size="5" /> &nbsp;</td>

<td><input name="discount[]" type="text" class="txtBox" id="discount[]" size="5" /> &nbsp;</td>

<td><input name="total_price[]" type="text" class="txtBox" id="total_price[]" size="5" /> &nbsp;</td>
<tr>

When I enter a value in any of the 3 boxes, I want it to recalculate and add rent_price & booking_costs and then deduct the discount as a percentage and put the total in total_price box
e.g
rent_price =900
booking_costs =100
discount=10%

total_price=900


Many thanks - Martyn.




Responses

Posted by: Jigarbagadai on: 9/27/2012 [Member] Starter | Points: 25

Up
0
Down
yes you can do it using below code..

<script type="text/javascript">

$(document).ready(function ()
{
$('.txtBox').change(function () {
var rentprice = parseFloat($('#rent_price').val());
var bookingcosts = parseFloat($('#booking_costs').val());
var discountrate = parseFloat($('#discount').val());
var rentandbookingcost=rentprice + bookingcosts;
var totalprice = rentandbookingcost - (rentandbookingcost * (discountrate / 100));
$('#total_price').val(totalprice);

});
});
</script>


Jigar Bagadai

Merlin, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Merlin on: 9/28/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Jigar,
Many thanks for the reply -I tried this but couldn't get it to go - but I did get a little further

http://jsfiddle.net/G9GdL/8/

The only part I cannot figure is how to seperate out the discount to remove it as a percentage from the total.

All help is greatly appreciated - I have been a programmer for 30 years but this JS stuff I really need to start at the bottom as the syntax notation is so dependant on casting

Regards
Martyn

Merlin, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response