Skip to main content
Known Participant
February 2, 2007
Answered

Javascript numeric & string problem

  • February 2, 2007
  • 2 replies
  • 344 views
Hi everyone,
I have a form, which takes two fields as numeric values, and with javascript function the sum of these two values should be displayed after any change. but instead of SUM , it does CONCATENATE on these two values. how do I tell Javascript function that these are numeric, not string?
thanks for your help in advance.
(I know it is a CF section, but I think people in CF use js as well)
    This topic has been closed for replies.
    Correct answer
    var a = 1;
    var b = 2;

    var sum = parseInt(a) + parseInt(b);

    (there's also a parseFloat() if you're not dealing exclusively with ints)

    2 replies

    Inspiring
    February 3, 2007
    Not so fast there. I had to develop some client side math on form fields once and parseInt and parseFloat were simply not good enough.

    What are the desired answers and expected answers for the following variables:

    var a = "x";
    var b = "8";

    var a = "42a";
    var b = "3";
    Correct answer
    February 2, 2007
    var a = 1;
    var b = 2;

    var sum = parseInt(a) + parseInt(b);

    (there's also a parseFloat() if you're not dealing exclusively with ints)