Created
September 15, 2015 16:28
-
-
Save MasterAlish/bfa95ee3888fdf7c5eac to your computer and use it in GitHub Desktop.
JS insert letter into input at the selection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
insert_letter: function (input_id, letter) { | |
var elem = document.getElementById(input_id); | |
var selectionStart = elem.selectionStart; | |
var selectionEnd = elem.selectionEnd; | |
var start = elem.value.substring(0, selectionStart); | |
var finish = elem.value.substring(selectionEnd, elem.value.length); | |
elem.value = start + letter + finish; | |
elem.focus(); | |
elem.selectionStart = selectionStart + 1; | |
elem.selectionEnd = selectionStart + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment