Nicky Hajal
Nicky Hajal

Reputation: 1644

How can I get the position of the cursor/caret within an HTML input element?

Should be pretty simple, but I can't seem to find it in Google.

Upvotes: 1

Views: 364

Answers (1)

IMTheNachoMan
IMTheNachoMan

Reputation: 5821

You would use the selectionStart or selectionEnd property of the input element.

setInterval(function(){
  console.log([document.getElementById("test").selectionStart, document.getElementById("test").

selectionEnd
------------

]);
}, 1000);
<input type="text" id="test"></input>

Upvotes: 1

Related Questions