Using js to scroll to caret pos in textarea in Firefox

Discussion in 'Javascript' started by rjonasz, Nov 21, 2006.

  1. rjonasz

    rjonasz Guest

    Hey Everyone,

    I'm trying to scroll a textarea to the position of the caret which is
    below the visible area of the textarea in firefox. I have code which
    works in IE but the code I use for firefox creates an elastic band
    effect. The textarea quickly scrolls down then bounces up to the top,
    no matter what I try.

    Has anyone run into the same difficulty? How did you solve it?

    This is the code I'm currently using:
    Note: inpu = textarea

    var ev = document.createEvent ('KeyEvents');
    ev.initKeyEvent('keypress', true, true, window,false, false, false,
    false, 0,inpu.value.charCodeAt(pos-1));
    inpu.dispatchEvent(ev); // causes the scrolling
    inpu.setSelectionRange(pos, pos);

    Thanks for your help!

    Randy
    rjonasz, Nov 21, 2006
    #1
    1. Advertising

  2. rjonasz

    rjonasz Guest

    On Nov 21, 2:46 pm, "rjonasz" <> wrote:
    > Hey Everyone,
    >
    > I'm trying to scroll a textarea to the position of the caret which is
    > below the visible area of the textarea in firefox. I have code which
    > works in IE but the code I use for firefox creates an elastic band
    > effect. The textarea quickly scrolls down then bounces up to the top,
    > no matter what I try.


    Well the solution to my problem was too simple. All I needed to do is
    call

    setTimeout("document.forms[0].txtarea.scrollTop="document.forms[0].txtarea.value.length",0);

    after i updated the textarea. Man there are days when the feeling of
    being dumb is matched by behaviour. :)

    Cheers,

    Randy
    rjonasz, Nov 22, 2006
    #2
    1. Advertising

  3. rjonasz

    Jonas Raoni Guest

    rjonasz escreveu:
    > setTimeout("document.forms[0].txtarea.scrollTop="document.forms[0].txtarea.value.length",0);


    This doesn't make sense, scrollTop is measured in pixels, do you want to
    scroll the textarea to the end? If yes, assign to the scrollTop, the
    value of the scrollHeight property.


    --
    Jonas Raoni Soares Silva
    http://www.jsfromhell.com
    Jonas Raoni, Nov 23, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. torch
    Replies:
    3
    Views:
    123
    Thomas 'PointedEars' Lahn
    Jun 20, 2004
  2. Paul Gorodyansky
    Replies:
    7
    Views:
    207
    Martin Honnen
    Feb 6, 2005
  3. Csaba  Gabor

    Textarea challenge: caret pos, resultant text

    Csaba Gabor, Apr 4, 2006, in forum: Javascript
    Replies:
    0
    Views:
    82
    Csaba Gabor
    Apr 4, 2006
  4. ReGenesis0
    Replies:
    3
    Views:
    372
    ReGenesis0
    Sep 28, 2008
  5. prati
    Replies:
    0
    Views:
    422
    prati
    Oct 27, 2012
Loading...

Share This Page