Menu

A JavaScript technical question

Wadim
2005-02-02
2013-04-22
  • Wadim

    Wadim - 2005-02-02

    I would like to the coordinates of caret or selection or recently
    typed character in a textArea control.

    I'm trying to display a context menu below the line with the caret,
    when a user presses a specific key or an accelerator sequence. The
    menu would dispay a list of suggested words to complete the typed
    word. It's code-completion.

    I've been thinking about the following solution:
    Is it posiible to get the coordinates by somehow getting the
    textRange of selected text only, or for example getting the
    textRange of the whole text in textArea and then retrieving the
    coordinates by counting the lines and characters from the beginning
    of text in textArea control to the caret positon.

    Anyway, how to retrieve these coordinates?

    Thanks in advance.

     
    • Luis Fernando Planella Gonzalez

      As far as I know, there is no standard way to retrieve the carret position on text fields or text areas.
      This sucks, and led to lots of extra code on the mask scripts, and even with that extra code, after typying, the cursor is positioned at the end... :-(

       
    • Luis Fernando Planella Gonzalez

      Well, I saw there's no standard way to do that.
      But, there are specific ways on specific browsers.
      Take a look at http://www.codingforums.com/archive/index.php/t-907
      Perhaps future versions of the mask will include this...

       
    • Luis Fernando Planella Gonzalez

      I have used some extra time I had and implemented some functions on JavaScriptUtil to manipulate a text selection / caret. They are only supported on Internet Explorer and Mozilla based browsers, and on IE requires a (unique) call to prepareForCaret(input).
      The masks now use this information too, allowing the user to type text on the middle of the mask and keep the caret where it was expected to be...
      Cool! :-)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.