Detect entered character with JavaScript
Use keypress
instead of keydown
. While keydown
relates to every press of a key, keypress
relates to the translated characters, so for example a
can be different to a
while the shift key is pressed, composed characters work, dead-keys work, and other differences in keyboard mappings are handled.
How about checking if @
was entered as the last character in the field value?
$("body").on("keyup", "textarea", function(e) {
if (this.value.indexOf("@") == this.value.length - 1) {
console.log("Starting autocomplete");
}
});
DEMO: http://jsfiddle.net/FKhPW/2/