Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the insertion point in a textbox from javascript?

I'm trying to insert text into an html textbox when the user pushes buttons. This is a simple on screen numeric keypad. I have found many different scripts claiming to be able to find the current cursor position in a textbox cross-browser, but none work in IE (I'm using IE8). Is this just an IE8 defect? Is there a workaround?

It seems like there must be a definitive answer about this somewhere, but I have looked far and wide to no avail. To reiterate, how do I find the current cursor position in an html textbox?

like image 638
Pete Avatar asked Aug 17 '09 17:08

Pete


2 Answers

look here:

http://javascript.nwbox.com/cursor_position/

like image 124
KM. Avatar answered Oct 24 '22 21:10

KM.


Pete, you can also save the caret position by using an "onblur" event so you always know the previous caret position.

like image 23
Diego Perini Avatar answered Oct 24 '22 21:10

Diego Perini