Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell the cursor/caret position in a number input element in firefox/chrome? [duplicate]

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" />
like image 942
Adam R. Grey Avatar asked Dec 10 '25 11:12

Adam R. Grey


1 Answers

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.

like image 111
choz Avatar answered Dec 13 '25 00:12

choz