I have a textarea in which I am inserting content at the location of the caret (thanks to Tim Down's answer). It inserts the content when the user presses a button. But it seems that when the button is pressed, the focus on the textarea is lost. How do I keep the focus there, providing the location of the caret is also the same? I was thinking along the lines of using evt.preventDefault()
with .focusout()
. If that helps.
Handle the mousedown-event instead of the click-event. The mousedown event will be handled before the focus of another element is lost.
In your mousedown eventhandler, you need to to prevent event default behavior.
e.preventDefault(); // in your mousedown eventhandler