gdoron gdoron - 4 months ago 23
Javascript Question

How can val() return Number?

In the

val
docs written this description:


.val() Returns: String, Number, Array


I tried to get a
Number
, but it seems to return
string
only, Is there something that I'm doing wrong?

HTML:

<input type="text" id="gdoron" />​


jQuery:

$('#gdoron').val('1');

alert($('#gdoron').val() === '1'); // true
alert(typeof $('#gdoron').val()); // string.

$('#gdoron').val(1);

alert($('#gdoron').val() === 1); // false
alert(typeof $('#gdoron').val()); // string (not "number"!)


My question is: how can
val()
return number as the docs says?
The question is not about how can I parse the string.


Answer

Some HTML5 elements e.g. progress;

<progress value="50" max="100" id="prog"></progress>
...
alert(typeof $("#prog").val()) // number