Skip to content

Instantly share code, notes, and snippets.

@gnarf
Created September 14, 2011 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnarf/1217560 to your computer and use it in GitHub Desktop.
Save gnarf/1217560 to your computer and use it in GitHub Desktop.
// assumptions:
// link = the markdown to inject
// textarea = jQuery() of the node
textarea.val( function( _, value ) {
var range,
begin = value.length;
if ( this.setSelectionRange ) {
begin = this.selectionStart;
} else if ( document.selection && document.selection.createRange ) {
range = document.selection.createRange();
// the moveStart returns the number of characters it moved as a negative number
begin = 0 - range.duplicate().moveStart( "character", -100000 );
}
return value.substr( 0, begin ) + link + value.substr( begin );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment