Last active
March 23, 2019 03:59
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$( '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