I'm trying to recreate the "check if hit" sistem from D&D in a small aplication and I need to get numeric values vrom input fields. Problem is, the normal document.queryselector('.input-class').value, only returns streengs. Any sugestions?
Asked
Active
Viewed 46 times
0
-
2Possible duplicate of [How to convert string into float in JavaScript?](https://stackoverflow.com/questions/642650/how-to-convert-string-into-float-in-javascript) – Calvin Nunes Aug 27 '19 at 18:54
-
2`parseInt(val)` – Jbluehdorn Aug 27 '19 at 18:54
2 Answers
0
Add + before value to convert to the number. Like:
let item = '2019';
assert(+item).toEqual(2019);
If you need some check, function isNaN() can be used after converting to number: https://www.w3schools.com/jsref/jsref_isnan.asp

Alex Vovchuk
- 2,828
- 4
- 19
- 40
0
yes you can cast the string with parseInt():
let text = '42px';
let my_number = parseInt(text, 10);
// returns 42

Klienblat Moshe
- 322
- 1
- 6