angelo
angelo

Reputation: 2951

How to get caret position in React?

I have a bunch of inputs.

I want to get the position of caret so that if user will press a left arrow at the start of one input element it will take it to the end of the previous input element and same for right arrow but to next input.

So far, I can't find a way to get the position of caret so that I can take appropriate actions.

Upvotes: 2

Views: 5782

Answers (1)

Sagar Acharya
Sagar Acharya

Reputation: 1871

What you can do is provided id to the input field. There is a property selectionEnd in the input element.

document.getElementById('inputElementId').selectionEnd

or you could use a ref on the input element to get the selectionEnd too.

You can use this to get the cursor position. And you can handle the logic to move to the other input field in a handler

Upvotes: 2

Related Questions