0

I have a input field with some text ( text length is 10 characters ). I want to get the cursor position where the user has tapped/clicked.

Suppose if user has tapped/clicked at 2 charcter then i want to get the position as 2.

Can anyone help me in achieving this with javascript/secnha/jquery?

Thanks in advance

  • What have you tried yet so far? See [how to ask](http://stackoverflow.com/help/how-to-ask/) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Bhojendra Rauniyar Dec 02 '15 at 11:14
  • Another duplicate: http://stackoverflow.com/questions/2897155/get-cursor-position-in-characters-within-a-text-input-field – freedomn-m Dec 02 '15 at 11:29

1 Answers1

0

Check this fiddle to see how relative position can be checked via jquery

and this fiddle to see how to calculate width of input text

so something like (not an executable code, just algo)

$( "input-field-selector" ).click( function(){

var positionXRelative = FromFirstFiddle();
var positionXRelative = positionXRelative - padding;
var indexOfClick = positionXRelative/sizeOfCharacterFromSecondFiddle();

} );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94