Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
jQuery.fn.extend insertAtCaret: (myValue) ->
@each (i) ->
if document.selection
sel = document.selection.createRange()
sel.text = myValue
else if @selectionStart or @selectionStart is "0"
startPos = @selectionStart
endPos = @selectionEnd
scrollTop = @scrollTop
@value = @value.substring(0, startPos) + myValue + @value.substring(endPos, @value.length)
@selectionStart = startPos + myValue.length
@selectionEnd = startPos + myValue.length
@scrollTop = scrollTop
@value += myValue
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.