0

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?

2 Answers2

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