Created
September 14, 2011 19:42
-
-
Save gnarf/1217560 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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