Skip to content

Instantly share code, notes, and snippets.

@StolenThunda
Last active March 23, 2019 03:59
Show Gist options
  • Save StolenThunda/99af15cdd7abd656948860cca025d3ef to your computer and use it in GitHub Desktop.
Save StolenThunda/99af15cdd7abd656948860cca025d3ef to your computer and use it in GitHub Desktop.
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 );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment