Skip to content

Instantly share code, notes, and snippets.

@MasterAlish
Created September 15, 2015 16:28
Show Gist options
  • Save MasterAlish/bfa95ee3888fdf7c5eac to your computer and use it in GitHub Desktop.
Save MasterAlish/bfa95ee3888fdf7c5eac to your computer and use it in GitHub Desktop.
JS insert letter into input at the selection
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