tags:

views:

18

answers:

1

Hello,

I have the following jQuery script:

$(document).ready(function() {
        $.ajax({
            url: 'temp.ajax.php',
            type: 'GET',
            success: function(data){
                $("#total_plus_aditional").val(data);                   
            }
        });

        tempTotal = parseFloat($("#total_plus_aditional").val());

        $("#cost_valoare").change(function(){                   
            var sum = parseFloat(tempTotal) + parseFloat($("#cost_valoare").val());
            $("#total_plus_aditional").val(sum);  
        });
});

Now, here's what I want to do: the temp.ajax.php returns a float value. First, that float value is assigned to an input field in the html so the user can see the number. Then, if the user enters a number in another input field, I would like to make the sum of those two numbers and rewrite the value in the first input field.

The problem is that if I write a number in a second field, the content of the first field (with the float number returned by ajax call) turns to NaN.

Why is this happening?

P.S. I'm saving the value returned by the ajax call in a separate variable because I would like to have the same value if the user changes his mind and enters another value in the second field after he already entered one.

+3  A: 
Pointy
@Pointy: Still returns NaN.
Psyche
Did you put it before or after the line where the input field's value is actually set?
Pointy
I got rid of that NaN thing, but now if the user enters another value, it adds to the previous computed value. This is not ok.
Psyche
Well if you do keep the "tempTotal" (original result) around, that should not be a problem. See the update. Revert the "change" handler to what you had - except *don't* call "parseFloat" on "tempTotal".
Pointy
Works now, thanks.
Psyche