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!
Similar Posts:
- how to get selected textarea value using javascript
- How to remove GET variable within $_SERVER['QUERY_STRING'] in PHP
- how to hide textarea?
- how to store and retrieve checkbox value in php?
- Firefox 3 gets stuck on typing in the address bar
- PHP – How to get domain name from URL?
- Free HTML Markup Editor
- How to use LOAD DATA INFILE to import fixed width data in MySQL
- Lightweight PHP WYSIWYG HTML Editor
- How to insert HTML/BBcode tags to WordPress Comments Form


December 30, 2008
Thanks, I had a similar problem while designing a web page and this helped me fix it.
March 26, 2009
Thanks!
March 20, 2010
YOU ARE A STAR!!! WORKS!!! SIMPLE AND EFFECTIVE!!!
I’ve been searching for this solution for a long time now!
THANK YOU!!!