Cursor position in textarea? (XY pos, not caret index)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ReGenesis0

    Cursor position in textarea? (XY pos, not caret index)

    Is it possible to determine the caret position, in terms of x/y
    pixels, within a textarea? (I want to have a suggestion box pop up
    under where you're typing... so i need to determine where you are
    typing.)

    -Derik

    (OTOH, finding the Caret position-- between what characters the cursor
    is positioned-- uses the function below. I include it to be nice to
    future searchers, and because it took me an hour of frustrated
    searching to find.)

    function getCaretPos(el) {
    if (typeof el.selectionSta rt != 'undefined')
    return el.selectionSta rt;
    else if (document.selec tion)
    return
    Math.abs(docume nt.selection.cr eateRange().mov eStart('charact er',
    -1000000));
    }
  • Thomas 'PointedEars' Lahn

    #2
    Re: Cursor position in textarea? (XY pos, not caret index)

    ReGenesis0 wrote:
    Is it possible to determine the caret position, in terms of x/y
    pixels, within a textarea? (I want to have a suggestion box pop up
    under where you're typing... so i need to determine where you are
    typing.)
    I do not think it is generally possible to determine the coordinates
    of the caret since you cannot always know which font is used.

    I would be confused if the suggestion box would move while I am typing, so
    it should suffice to calculate the offset position and height of the control.


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

    Comment

    • SAM

      #3
      Re: Cursor position in textarea? (XY pos, not caret index)

      Le 9/28/08 4:11 PM, ReGenesis0 a écrit :
      Is it possible to determine the caret position, in terms of x/y
      pixels, within a textarea? (I want to have a suggestion box pop up
      under where you're typing... so i need to determine where you are
      typing.)

      I don't know if ... but :
      <http://www.quirksmode. org/js/findpos.html>

      --
      sm

      Comment

      • ReGenesis0

        #4
        Re: Cursor position in textarea? (XY pos, not caret index)

        On Sep 28, 9:59 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
        wrote:
        ReGenesis0 wrote:
        Is it possible to determine the caret position, in terms of x/y
        pixels, within a textarea?  (I want to have a suggestion box pop up
        under where you're typing... so i need to determine where you are
        typing.)
        >
        I do not think it is generally possible to determine the coordinates
        of the caret since you cannot always know which font is used.
        *grumble*
        I would be confused if the suggestion box would move while I am typing, so
        it should suffice to calculate the offset position and height of the control.
        It's a *large* textarea box. Placing the popup at top or bottom would
        require scrolling the browser.

        Is there a method for determining the rows then? Just hovering the
        box below the current line would probably work...

        -Derik

        Comment

        Working...