Skip to content

Instantly share code, notes, and snippets.

@guillaumepiot
Created December 16, 2014 14: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 guillaumepiot/56191c0256f9ae9bf8e1 to your computer and use it in GitHub Desktop.
Save guillaumepiot/56191c0256f9ae9bf8e1 to your computer and use it in GitHub Desktop.
JAVASCRIPT - Set cursor positon in input and textarea
setSelectionRange: (input, selectionStart, selectionEnd)->
if input.setSelectionRange
input.focus()
input.setSelectionRange(selectionStart, selectionEnd)
else if input.createTextRange
range = input.createTextRange()
range.collapse(true)
range.moveEnd('character', selectionEnd)
range.moveStart('character', selectionStart)
range.select()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment