Skip to content

Instantly share code, notes, and snippets.

@NdYAG
Created August 3, 2013 06:49
Show Gist options
  • Save NdYAG/6145500 to your computer and use it in GitHub Desktop.
Save NdYAG/6145500 to your computer and use it in GitHub Desktop.
Get the character user input in input[type="text"] bypassing IME
function getCaretPos(input_node) {
return 'selectionStart' in input_node? input_node.selectionStart: Math.abs(document.selection.createRange().moveStart('character', -input_node.value.length))
}
function getCaretText(input_node) {
var pos = getCaretPos(input_node)
if(!pos) return
return input_node.value.substring(pos - 1, pos)
}
$input_node.on('keyup', function() {
dosomething(getCaretText($input_node[0]))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment