I have to call a function only if ajax call success.
Code:
<script>
function removeFromCart(id) {
var value = "";
for(var i=0; i<id.length; i++)
if(!isNaN(id[i]))
value += id[i];
$.ajax({
type: "POST",
url: "http://url.com/removefromcart.php",
data: "id="+value,
success: function(response){
afterRemove(response);
}
});
}
function afterRemove(response) {
if(response[0] == "r") {
var prezzo = "";
for(var i = 1; i<respose.length; i++)
var prezzo += response[i];
document.getElementByid("prezzo").innerHTML = prezzo+".00 €";
$("#segnale"+value).fadeOut();
}
}
</script>
The problem is that Firebug gives me this error on page load:
invalid variable initialization: var prezzo += response[i];
This because the afterRemove function is called on page load and not only if ajax success. How can fix this?
resposeinstead ofresponsein yourforinitialization.$("#segnale"+value)) lookups and non-jQuery lookups (document.getElementByid("prezzo")).$("#prezzo").html(prezzo+".00 €");