Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.