Why <input type="number"> format is a text (string)? Not a number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why <input type="number"> format is a text (string)? Not a number?

https://code.sololearn.com/Wj7EiI4oXZLY/?ref=app

10th Feb 2018, 9:15 AM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
8 Answers
+ 3
I found on Google .... replace .value with . value AsNumber
10th Feb 2018, 11:24 PM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 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.
10th Feb 2018, 9:46 AM
Vitaliy Angelov (Rostov-on-Don)
Vitaliy Angelov (Rostov-on-Don) - avatar
+ 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.☺😋😋😋😋
10th Feb 2018, 10:40 AM
Vitaliy Angelov (Rostov-on-Don)
Vitaliy Angelov (Rostov-on-Don) - avatar
+ 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.
10th Feb 2018, 12:54 PM
Vitaliy Angelov (Rostov-on-Don)
Vitaliy Angelov (Rostov-on-Don) - avatar
+ 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?
10th Feb 2018, 1:10 PM
Vitaliy Angelov (Rostov-on-Don)
Vitaliy Angelov (Rostov-on-Don) - avatar
+ 7
@Didi Georgel Danaila You are well done!💪👍Thanks for answer. I'll remember it for future.
11th Feb 2018, 5:11 AM
Vitaliy Angelov (Rostov-on-Don)
Vitaliy Angelov (Rostov-on-Don) - avatar
+ 5
@Vitaly if it is a number why is not working this var add = firstNumber + second Number; in my code???
10th Feb 2018, 12:31 PM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 3
so how can access a <input type= number> to return a number without use parseInt() ?
10th Feb 2018, 11:05 PM
Didi Georgel Danaila
Didi Georgel Danaila - avatar