Dec
Fix -> Caret/Cursor jumps to start of textarea in javascript
I was working on a javascript bbcode editor (textarea based) and often if the textarea contents are long and automatic scrollbars are enabled. If i replace selected code in textarea using javascript often the cursor jumps to start of the textarea and focus is lost. This problem only happens in firefox and works fine in IE.
This is a very annoying problem and everytime you have to scroll down to insert changes to your textarea content.
Here is you fix it.
Store the scroll position in a variable and you replace selected text, bring back the scroll to the original position. This 3 lines of code will solve the problem for firefox.
var scrollTop = textarea.scrollTop; var scrollLeft = textarea.scrollLeft; objTextarea.scrollTop = scrollTop; objTextarea.scrollLeft = scrollLeft;
I struggled a lot get this fix for simplest of problems and it was less documented on the web.
Hope this helps!



















December 30th, 2008 at 8:04 pm
Thanks, I had a similar problem while designing a web page and this helped me fix it.
March 26th, 2009 at 3:58 pm
Thanks!