StolenThunda
3/23/2019 - 3:58 AM

Adds text into a text area at the user's cursor position or replaces selected text. (https://stackoverflow.com/questions/11076975/insert-te

Adds text into a text area at the user's cursor position or replaces selected text. (https://stackoverflow.com/questions/11076975/insert-text-into-textarea-at-cursor-position-javascript)

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