0

I am trying to calculate Body Mass Index by receiving Weight and Height from HTML form and passing it to JavaScript code by .getElementById();. But, as far as I understand, type of data received from form is "string" and I need to receive a "number". How can I solve this problem?

3 Answers3

1

Use parseInt().

var a = "10";
var b = parseInt(a);

See : http://www.w3schools.com/jsref/jsref_parseint.asp

Léo Martin
  • 736
  • 7
  • 13
0
var a = document.getElementById("whatever").value;

// You can use any 3
+a; // Unary Operator
parseInt(a, 10); // parseInt with radix 10
Number(a);
void
  • 36,090
  • 8
  • 62
  • 107
0

you can use function parseInt(string) that Convert string into int .Now in your senario :- var weight_str = document.getElementByID("weight"); var weight_int = parseInt(weight_str);

this will give you the result.