views:

62

answers:

4

Hey guys. I don't know much JS, but I wanted to do some quick work with jQuery.

But I've been staring at this for about an hour and I don't understand what I missed:

<script type="text/javascript">
    $('#qty_6035').change(function () {
        var substractedQty, stockQty, remQty;
        substractedQty = (int) $('#qty_6035').val(); // missing ; before statement 
        stockQty = (int) $('#orig_qty_6035').val();
        $('#rem_qty_6035').html(stockQty-substractedQty);
    });
</script>

jQuery library is included at the beggining of the document.

Thanks.

+4  A: 

Javascript is a dynamic language so in order to convert a string into a number you could use the parseFloat/parseInt functions:

<script type="text/javascript">
    $('#qty_6035').change(function () {
        var substractedQty = parseFloat($('#qty_6035').val());
        var stockQty = parseFloat($('#orig_qty_6035').val());
        $('#rem_qty_6035').html(stockQty - substractedQty);
    });
</script>
Darin Dimitrov
+6  A: 

Use parseInt function, not (int) casting

streetpc
Be sure to use the `radix` parameter to force base 10 when using `parseInt()` --- `parseInt($("#qty_6035").val(), 10);`
gnarf
+4  A: 

JavaScript is not Java. int is a reserved keyword but doesn't have any functionality assigned to it, and you can't cast a value that way.

You probably want:

substractedQty = parseInt($('#qty_6035').val(), 10);
David Dorward
Arghhhh!!! That error is so damn confusing :)Thanks all!
Bogdan
+3  A: 

Javascript doesn't support type casting like strong typed languages (C#, Java) do. To convert the field values (which are strings) to numbers you need to use the global functions parseInt() or parseFloat().

You'll probably also want to make sure the values are parsed correctly, in case a user entered some bad input instead of a number. Use isNAN() for that.

Marnix van Valen