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")
Input value is a string instead of a number.
It's normal you get a string. The purpose of the number type is that mobile browsers use this for showing the right keyboards and some browsers use this for validation purposes. For example the email type will show a keyboard with the @ and '. ' on the keyboard and number will show a numeric keyboard.
To check if an input's value is a valid number in React: Check if the input's value is not an empty string or contains only spaces. Pass the value to the isNaN() function. If isNaN returns false , the value is a valid number.
< input type = "button" value = "Submit" >
Check out HTMLInputElement.valueAsNumber
:
The value of the element, interpreted as one of the following in order:
- a time value
- a number
- null if conversion is not possible
var testInput = document.getElementById("test-input");
function handleInput(e){
var value = this.valueAsNumber;
console.log("type: %s, value: %o", typeof value, value);
}
testInput.addEventListener("input", handleInput);
handleInput.call(testInput);
<input type="number" id="test-input" type="number" value=5>
This will return NaN
for non-numerics or non-finite numbers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With