JavaScript TypeError Function

I have JavaScript code:

var update_money = function(money_am){
    var d = {'ammount': money_am};
    $.post("/adr/", d, load_money)
}

z = parseFloat($('#money').val());
$('#money').change(z, update_money);

      

When it is executed, it gives me a runtime error:

TypeError: "stopPropagation" invokes an object that does not implement the Event interface.

In debugging I found that money_am

it is not a float. It is an object. But if I change my code like this:

var update_money = function(money_am){
    var d = {'ammount': parseFloat($('#money').val())};
    $.post("/adr/", d, load_money)
}

      

It works great. What can I do to fix this problem?

+3


source to share


2 answers


The data that was passed to the event handler can be accessed with event.data

:

Fiddle .



var update_money = function(event)
{
    var money_am = event.data;
    alert(money_am);
    var d = {'ammount': money_am};
    $.post("/adr/", d, load_money);
}

function load_money() { }

z = parseFloat($('#money').val());
$('#money').change(z, update_money);

      

+2


source


Make sure the monetary value is handled by parseFloat:

You can also change your code to this:



$('#money').change(function() {
    var z = parseFloat($('#money').val());
    load_money(z);
});

      

This will allow you to receive a new cash value every time the money field changes. Instead of always using the same value. Along with the correct definition of z.

0


source







All Articles