Textarea tags
Insert a new text or a input value into a textarea at the cursor position using jquery
HTML
<form>
<textarea id="text" cols="40" rows="3"></textarea>
<input type="button" value="[tag_label]" />
</form>
JavaScript
$( 'input[type=button]' ).on('click', function(){
$("#text").insertIntoTextArea($(this).val());
});
$.fn.insertIntoTextArea = function(textToInsert) {
return this.each(function () {
var txt = $(this);
var cursorPosStart = txt.prop('selectionStart');
var cursorPosEnd = txt.prop('selectionEnd');
var v = txt.val();
var textBefore = v.substring(0, cursorPosStart);
var textAfter = v.substring(cursorPosEnd, v.length);
txt.val(textBefore + textToInsert + textAfter);
txt.prop('selectionStart', cursorPosStart);
txt.prop('selectionEnd', cursorPosStart + textToInsert.length);
txt.focus();
});
}