Is it possible to bind Ctrl+Alt+L to insert predefined string into focused textarea to cursor position? For example, I’m typing some text in textarea, then I’m pressing Ctrl+Alt+L and <a href="" title=""></a> is inserted into current cursor position.
Is it possible to bind Ctrl + Alt + L to insert predefined string
Share
Yes. Use the
keydownevent and one of the caret-position-in-textarea functions floating around on Stack Overflow. For the key detection, note I’ve had to use thekeydownevent rather than thekeypressevent (which is what should be used for detecting what character has been typed) because IE doesn’t fire thekeypressevents for Ctrl+Alt+L, so this could go wrong on differently mapped keyboards. For the cursor position, I’ve copied from this answer and have used something similar myself. See these answers for discussion of the problems with this in IE:Caret position in textarea, in characters from the start
Is there an Internet Explorer approved substitute for selectionStart and selectionEnd?
Also, note that you may want to position the cursor somewhere sensible after doing this, which my code doesn’t cover.