I have a textarea
in which the user can type text. The user can also push a button to add stuff like images and links. This button namely prompts them for their link, and then inputs the correct html in the textarea where the cursor was located. A working code example is this:
jQuery('input#btn').click(function(e) {
var link = prompt("Please paste the link", "http://");
jQuery('textarea#id1').insertAtCaret('<a href="'+link+'">link text</a>');
});
The .insertAtCaret()
function is the one kindly created by Daniel Beck for another question of mine. It finds the position of the cursor in the textarea, so that the html-chunk can be added here.
But after jQuery has added this html-chunk, the cursor is no longer in the textarea, but focus is now on the button that was pushed. I would like the cursor to remain where is was before pushing this button (after the added html-chunk).
Is it possible to find the last inputted text in a textarea and is it then possible to move the curser there?
Note, this input could be anywhere in the already written text in the textarea
.