Skip to content

Instantly share code, notes, and snippets.

@zachaysan
Created October 22, 2013 13:08
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 zachaysan/7100458 to your computer and use it in GitHub Desktop.
Save zachaysan/7100458 to your computer and use it in GitHub Desktop.
insertAtCaret
jQuery.fn.extend insertAtCaret: (myValue) ->
@each (i) ->
if document.selection
@focus()
sel = document.selection.createRange()
sel.text = myValue
@focus()
else if @selectionStart or @selectionStart is "0"
startPos = @selectionStart
endPos = @selectionEnd
scrollTop = @scrollTop
@value = @value.substring(0, startPos) + myValue + @value.substring(endPos, @value.length)
@focus()
@selectionStart = startPos + myValue.length
@selectionEnd = startPos + myValue.length
@scrollTop = scrollTop
else
@value += myValue
@focus()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment