I'm trying to emulate <input type="text">
to behave like <input type="number">
(up/down arrow keys increase/decrease value)
So far it works fine, except when value reaches Number.MAX_SAFE_INTEGER
(9007199254740992) it stops increasing, but type="number"
can go beyond that number.
Is there a way I can somehow bypass this limitation in javascript?
const input = document.querySelector('input[type="text"]'),
step = {ArrowUp: 1, ArrowDown: -1};
input.addEventListener("keydown", (e) =>
{
if (step[e.key])
{
input.value = Number(input.value) + step[e.key];
return e.preventDefault();
}
});
<table>
<tr>
<td>type="text"</td>
<td><input type="text" value="9007199254740990"></td>
<td>use UP/DOWN arrow keys</td>
</tr>
<tr>
<td>type="number"</td>
<td><input type="number" value="9007199254740990"></td>
</tr>
</table>
As per @CertainPerformance answer BigInt()
seems to be a good work around, however it doesn't allow use float numbers.
Also, with float numbers if step
is set to for example 1.4, it shows some weird numbers like 4.199999999999999