Textarea tags

Insert a new text or a input value into a textarea at the cursor position using jquery

by juErik

HTML

<form>
    <textarea id="text" cols="40" rows="3"></textarea>
    <input type="button" value="[tag_label]" />
</form>

JavaScript

$( 'input[type=button]' ).on('click', function(){
            var cursorPos = $('#text').prop('selectionStart');
            var v = $('#text').val();
            var textBefore = v.substring(0,  cursorPos );
            var textAfter  = v.substring( cursorPos, v.length );
            $('#text').val( textBefore+ $(this).val() +textAfter );
        });