I have <textarea>
where user can type in his message to the world! Below, there are upload buttons... I would love to add link to uploaded file (don't worry, I have it); right next to the text that he was typing in.
Like, he types in 'Hello, world!', then uploads the file (its done via AJAX), and the link to that file is added in next line to the content of <textarea>
. Attention! Is it possible to keep cursor (place where he left to type) in the same place?
All that may be done with jQuery... Any ideas? I know that there are method 'append()', but it won't be for this situation, right?
Try
You can use any of the available caret plugins for jQuery and basically:
If you want an "append" function in jQuery, one is easy enough to make:
Then you can use it by making a call to
.valAppend()
, referencing the input field(s).You may take a look at the following answer which presents a nice plugin for inserting text at the caret position in a
<textarea>
.This took me a while, but the following jQuery will do exactly what you want -- it not only appends text, but also keeps the cursor in the exact same spot by storing it and then resetting it:
You should probably wrap this in a function though.
You could use my Rangy inputs jQuery plugin for this, which works in all major browsers.