How can I use
$('div').bind('keyup', function(e) {
if(e.which == '@') {
}
});
Wondering how I can get the @
symbol for keyup
?
How can I use
$('div').bind('keyup', function(e) {
if(e.which == '@') {
}
});
Wondering how I can get the @
symbol for keyup
?
Use the String.fromCharCode
method. The reverse function is "@".charCodeAt(0)
.
The e.which
will only hold sensible data about the pressed key at a keypress
event. This event will trigger multiple times though, while a key is pressed down.
If you need a reliable method to check the character of a key during the keyup
event, create a character map. This page will aid you.
$('div').bind('keypress', function(e) {
var char = String.fromCharCode(e.which);
if(char == '@') {
}
});
You could try yourself before post such question
Go to JSBin and do a simple test:
Then you will know exactly what to look for.
by the way, because you need to worry about plenty on all mobile devices and browsers, I will suggest that you use indexOf()
or a variant of it, for example:
$("#txt").bind("keyup", function(evt) {
if( $(this).val().indexOf("@") > 0 ) {
// got it
}
});
or if you need to know if it's the last character written:
if( $(this).val().lastIndexOf("@") == $(this).val().length - 1)
{
// got it
}
This seems to be working.. http://jsfiddle.net/jNeH9/2/
$(document).bind('keyup', function(e) {
if(e.which == 50 && e.shiftKey)
{
}
});