2

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" />
Adam R. Grey
  • 1,861
  • 17
  • 30

1 Answers1

1

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.

choz
  • 17,242
  • 4
  • 53
  • 73
  • Yes, and in IE, it doesn't matter that the type is number, the input will eat characters so if only numbers need, js validator is better for it. – DiabloSteve Sep 13 '18 at 15:10