Reputation: 711
I've got a nice little shopping basket where if the user deletes an item, the slideup jquery function is called and is disappears. In the back ground, I'm removing it from the db with a call to a code igniter controller.
function delete_booking(id_booking, amount) { if (user_total == null || user_total == '') { var user_total = parseFloat(0); } $.ajax({ type: "POST", data: '', url: "/"+id_booking, }); $("#booking_id_"+id_booking).slideUp(); var user_total = (parseFloat() - parseFloat() - parseFloat(user_total)); alert(user_total); alert(parseFloat(user_total)); $('#booking_total').html(user_total); }
What I can't fathom, is when I update the user total: $('#booking_total') I need somehow to have the function remember the NEW total... I've googled 'remembering JavaScript var' and similar, but I can't find exactly what I need... My only other option is to do another call to the db with ajax to get the new total, but there MUST be a JS way to do this?
Upvotes: 1
Views: 1217
Reputation: 21881
Because I "hate" a global approach for this type of problem, here is a little closure which will do the job of "remembering" the value (although I'm not really sure what exactly should be saved...^^)
var delete_booking = (function() {
var booking_total = $("#booking_total"),
total_amount = parseFloat(booking_total.html()) || 1000; // what ever the value should be...
return function(id, m) {
$.ajax(/* ... */);
$("#booking_id_"+id).slideUp();
total_amount -= m;
booking_total.html(total_amount);
}
}());
Upvotes: 0
Reputation: 32107
as you have said
I need somehow to have the function remember the NEW total
a function cant remember a value, while a variable can, to solve your problem do one the below
1
var finalTotal;
function delete_booking(...)
{
//your more codes
$('#booking_total').html(user_total);
finalTotal = user_total;
}
now variable finalTotal
hold the value
2
whenever you feel that you need to access the total read this way
var total=parseint($('#booking_total').html(),10) || 0;
But think method 1 is better than method 2
Upvotes: 0
Reputation: 2690
If you are not navigating to a different page, you can store the value in a variable that is outside the function (whether just on the page in the global namespace or the within the module in which the function resides).
var total;
function delete_booking(...)
{
...
total = user_total;
}
Upvotes: 3