+ 5
Why <input type="number"> format is a text (string)? Not a number?
8 Réponses
+ 3
I found on Google .... replace .value with
. value AsNumber
+ 11
It's a good question. 👍 I've tried to solve it , but I couldn't. I've added id="inp1" to the first input and then added such string to the body of script:
alert(document.getElementById("inp1"). typeof);
and it alerted 'undefined'.
I'm confused a little. It's very interesting to clear this question for me too!😋☺
I'll try to clear it and if I'll be successful then I'll answer to you here at the post.
+ 8
I've solved it! There was a mistake in my string.
add: id="inp" to the first input.
then add such string to the script:
alert(document.getElementById("inp").type);
It will alert 'number'.
So, the type of your inpute type="number" is number.☺😋😋😋😋
+ 8
@Didi George Danaila I'll try to find out it why. But it's the number 100%. I've checked it with the type.
+ 8
@Didi Georgel Danaila I found out why it is not worked right. See, the input type is number(we checked it with the type), but when you write such string of code:
var firstNumber = document.myForm.firstNumber.value;
var secondNumber = document.myForm.secondNumber.value;
.value returns the string. So you varriables become string type and afterwards you write such code:
var add= firsrNumber + secondNumber;
it will do adding as strings. So , you need to add parseInt(as you made already) or Number to change the value of varriables to Number.
Is it clear now?
+ 7
@Didi Georgel Danaila You are well done!💪👍Thanks for answer. I'll remember it for future.
+ 5
@Vitaly if it is a number why is not working this
var add = firstNumber + second Number;
in my code???
+ 3
so how can access a <input type= number> to return a number without use parseInt() ?