4

I have this text on an input element: BT25 3___ ____ ____ ____.

I need to know the index of the character where I clicked with the mouse. More specific I need to find what is the index of the clicked value?

It need to be functional in IE11.

1
  • 1
    in IE, isn't there an input.selectionStart or input.selStart? Commented Mar 20, 2017 at 9:03

1 Answer 1

8

Use selectionEnd on mouseup event:

document.querySelector("input").addEventListener("mouseup", function(e){
  console.log("pos:", this.selectionEnd);
});
<input value="BT25 3___ ____ ____ ____">

There's no compatibility problem, it even works on IE9.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. One more question, I saw that the selectionEnd and selectionStart works with the "onclick" and 'mouseup' events. Is it possible to make it work and on a keyboard event?
@ttdol2506 You don't even need an event, input.selectionStart and input.selectionEnd are always available on the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.