jQuery: Get the cursor position of text in input without browser specific code? [duplicate]
Asked Answered
N

4

30

Is there any way in jQuery to get the current cursor-position in an text input without doing ugly browser specific code?

Like in:

<input id="myTextInput" type="text" value="some text" >

the cursor being after the "x" of "some text", I can get "8"?

Nomology answered 3/11, 2010 at 8:43 Comment(2)
related : #2897655Prelect
Sorry, the input field was not showing in the beginning. That should tell what I want.Nomology
B
36

You can't do this without some browser specific code, since they implement text select ranged slightly differently. However, there are plugins that abstract this away. For exactly what you're after, there's the jQuery Caret (jCaret) plugin.

For your code to get the position you could do something like this:

$("#myTextInput").bind("keydown keypress mousemove", function() {
  alert("Current position: " + $(this).caret().start);
});

You can test it here.

Bumboat answered 3/11, 2010 at 10:40 Comment(6)
This Jquery Caret link plugins.jquery.com/project/jCaret is down.Dugger
Link to the jCaret project at Google Code http://code.google.com/p/jcaretMagistery
@nickcraver Not working on my code. Do I need to add another source like <script type="text/javascript" src="ajax.googleapis.com/ajax/libs/jquery/1.6/…> ?Thirtieth
Yeah, you need jQuery.Fleisig
.start is undefined in ie11. You need to use .beginDanitadaniyal
I believe the link is now: bitbucket.org/accursoft/caret/srcCardoso
S
18

Using the syntax text_element.selectionStart we can get the starting position of the selection of a text in terms of the index of the first character of the selected text in the text_element.value and in case we want to get the same of the last character in the selection we have to use text_element.selectionEnd.

Use it as follows:

<input type=text id=t1 value=abcd>
<button onclick="alert(document.getElementById('t1').selectionStart)">check position</button>

I'm giving you the fiddle_demo

Suggestibility answered 19/9, 2013 at 19:49 Comment(3)
Why do you bother putting el.value into var val? Abbreviating such an explicit term to only use it once seems just to confuse otherwise nice code (I've not run it yet :)Bronez
@LeeGee true and hence reduced the code.Suggestibility
Great solution! It doesn't work in IE8, but who cares!Fettle
A
16

A warning about the Jquery Caret plugin.

It will conflict with the Masked Input plugin (or vice versa). Fortunately the Masked Input plugin includes a caret() function of its own, which you can use very similarly to the Caret plugin for your basic needs - $(element).caret().begin or .end

Awhile answered 2/10, 2011 at 12:36 Comment(1)
Also, the jsfiddle numbers do not look correct in the case where one might press the down/up keys. The cursor swings to the end of the text box but the value is not representative of such moves to the start/end of the input.Isleana
P
7

just came across while browsing, might help you javascript-getting-and-setting-caret-position-in-textarea. You can use it for textbox also.

Paternity answered 3/11, 2010 at 8:50 Comment(1)
Unfortunately this website is no longer accessible. Do you remember what the implementation involved?Gearwheel

© 2022 - 2024 — McMap. All rights reserved.