Skip to content

Instantly share code, notes, and snippets.

@maximkoretskiy
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save maximkoretskiy/191c49fb7c361bdac3b4 to your computer and use it in GitHub Desktop.

Select an option

Save maximkoretskiy/191c49fb7c361bdac3b4 to your computer and use it in GitHub Desktop.
Textarea start editing prevention
# demo: http://jsfiddle.net/q173wuoy/1/
KEY_DELETE = [8, 46]
KEY_NAV = [33..40]
$( ->
inputPreventer = (e)->
length = 10
cursorPos = e.currentTarget?.selectionStart
return if e.keyCode in KEY_NAV
border = if e.keyCode in KEY_DELETE then length+1 else length
e.preventDefault() if 0 < cursorPos < border
($ 'textarea').on 'keydown', inputPreventer
)
<textarea class='ta' name="" id="" cols="30" rows="10">Жуй тебе, дружок</textarea>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment