When i add a number to the input it doesn't change the value that stays at 5 and when i go in the console and type uw.value i get "6" back instead of 6 which i typed in the input on the web how do i get a number back instead of a string?
<form>
<input type="number" id="wakken" type="number" value=5><br>
<input max="30" min="0" name="ijsberen" placeholder="Ijsberen" id="ijsberen" type="number" value= "5"><br>
<input max="30" min="0" name="wakken" placeholder="Penguins" id="penguins" type="number" value= 0><br>
<input type="button" value="submit" onclick="check()">
</form>
var uw = document.getElementById("wakken")
var ui = document.getElementById("ijsberen")
var up = document.getElementById("penguins")
Since input
HTMLElement.value
returns a "String"....jsbin demo
Convert your
"String" number
toNumber
by usingor by simply adding a
unary +
before the value:To be totally sure your
value
is a number you can also do:For example:
Don't forget to: read the Docs
parseInt()
parseFloat()
Number()
Arithmetic operators
You can convert the value from string to number using
parseFloat(value)
for decimals orparseInt(value)
for integers.For example:
Check out
HTMLInputElement.valueAsNumber
:This will return
NaN
for non-numerics or non-finite numbers.Will log "works! 'uw' is an int", because we collected the value and used
parseInt(_: String)
to convert it to an int.