so there's this: https://stackoverflow.com/a/48150864/1173856 but it doesn't work in Firefox or Chrome when the input type is a number. In those cases selectionStart and selectionEnd are both null.
document.getElementById('foobar').addEventListener('keyup', e => {
console.log('Caret at: ', e.target.selectionStart)
})
<input id="foobar" type="number" />
According to the spec selectionStart only works for text, search, tel, url and password.
Perhaps you can use text instead and use regex to accept only numbers.
There is another post which has already discussed this. There should be alternative ways you can implement to help for your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With