Need help firing an event only once using .change()
Hey Guys. I have created a "Tip" selection drop down menu for a restaurant ordering website. When the user reaches the checkout page, it will automatically add the default tip of $2.00 to the subtotal amount. Here is the function will execute when on a document ready event
- function changeAmountsBasedOnTip(){
var $tip = $('#tip').val(); // Select the value of the tip
var $subtotal_elm = $('#sub_total').text() Select the value inside the subtotal div element
var $subtotal = returnAmount($subtotal_elm) //Return only the amount and not the text
var $tax_elem = $('#sales_tax').text(); //Select the value inside the sales tax div element
var $sales_tax = returnAmount($tax_elem); //Return only the amount not the text
//Add the subtotal tax and tip together
var $grand_total = parseFloat($subtotal) + parseFloat($sales_tax) + parseFloat($tip) ;
var $rounded_grand_total = Math.round($grand_total * 100) / 100; //Round the total
$('#grand_total').text("Grand Total = " + $rounded_grand_total); //Insert it $grand total div element
}
The challenge I am having is subtracting the tip amount from the total when some select "Pick Up"
I have wrote something that does that, however it continuously subtracts the total amount from the tip every time "Pick Up" drop down is selected.
For example if the total amount is $7.00 and the defualt tip is $2.00. That will bring me a total of $9.00
If I select pick up once the $9.00 get subtracted from $2.00, and brings me back to the subtotal with out tip ($7.00)
That is great! But when I select pick up a second time it goes down to $5.00, then to $3.00 etc etc. All I want is the tip to be subtracted one time from the total when pick up is selected
Below is the code that I currently have when Pick Up Is selected
Please not the code below may have some syntax errors as a just quickly copied and pasted it.
Any help will be really appreciated. Thanks!
- $("#delivery_method").change(function() {
if($(this).val() == "Pick Up") {
// The following keeps subtracting the total amount from the tip every time it get selected
var $grand_total_return = $("#grand_total").text();
var $tip = $("#tip").val();
var $grand_total_amount_format = returnAmount($grand_total_return);
var $total_without_tip = $grand_total_amount_format - $tip;
var $rounded_total_without_tip = Math.round($total_without_tip * 100) / 100;
$("#grand_total").text("Grand Total = " + $rounded_total_without_tip);
- //This event handler just hides all the input fields that is needed for a delivery order
$("#address, #suite, #cross_streets,#glbladdress,#gaddress,#checkout_tip,#tip").prop({disabled:true}).hide(); // First hide all the input fields that is needed for a delivery up order and just show the input fields needed for a pick up order
} else {
$("#address, #suite, #cross_streets,#glbladdress,#gaddress,#checkout_tip,#tip").prop({disabled:false}).show(); // Show input fields needed to complete a delivery order
- }