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
\tor 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
\tbut caret pos will go to the end and all text in textarea are selected - Chrome will not count this
\tas user input soCTRL+Zwill undo pre-last operation, leaving\tin 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"?