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!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • feedmelinks
  • Furl
  • Fleck
  • MyShare
  • NewsVine
  • Netscape
  • Slashdot
  • Spurl
  • StumbleUpon
  • Taggly
  • Technorati
  • Wists
  • YahooMyWeb