Raktim Posted August 29, 2019 Share Posted August 29, 2019 I write an HTML code that takes input from the user which type="number". I also keep the user input value into a js variable. But when I check the data type of that variable it shows string...WHY? <html> <head> </head> <body> <input type="number" id="mynumber" value="2"><br> <p id="demo"></p><br> <button type="button" onclick="foo()">click here</button> <script> function foo () { var c = document.getElementById("mynumber").value; document.getElementById("demo").innerHTML = typeof c ; } </script> </body> </html> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 29, 2019 Share Posted August 29, 2019 The type attribute is just for native validation or adding helpful controls (like a date picker), but according to MDN the value property is always a string: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement Link to comment Share on other sites More sharing options...
Raktim Posted August 29, 2019 Author Share Posted August 29, 2019 Okay Sir, Thank you Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now