There are many questions about how to prevent focusing next element, when pressing TAB
in textarea tag and just insert a \t
character. Or any other character. All of them are answered with something like:
- Capture a TAB keypress
- Get current caret position and selection info
- Replace the value so it will contain
\t
or whatever user wants.
All this works fine until you'll press "Undo" or CTRL+Z
after TAB
If you do this, you will experience some side effects depending on the browser you use:
- Firefox will remove
\t
but caret pos will go to the end and all text in textarea are selected - Chrome will not count this
\t
as user input soCTRL+Z
will undo pre-last operation, leaving\t
in place, there is also some caret problems which are hard to predict - IE is acting same as Chrome
You can watch this here http://jsfiddle.net/c7zc8/1/
The question is how to make it crossbrowser and "undoable"?