Using js to scroll to caret pos in textarea in Firefox

R

rjonasz

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
 
R

rjonasz

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
 
J

Jonas Raoni

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top