I have been trying to get this to work but I don't know what's going on, the code I have:
$('#buscar-producto').on('keydown', function(e){
console.log('hello');
console.log(e.keyCode);
});
It works on computers, but not on mobile..
EDIT: I need to get the keyCode when a key is pressed...
I have been trying to get this to work but I don't know what's going on, the code I have:
$('#buscar-producto').on('keydown', function(e){
console.log('hello');
console.log(e.keyCode);
});
It works on computers, but not on mobile..
EDIT: I need to get the keyCode when a key is pressed...
Share Improve this question edited Nov 24, 2017 at 21:14 jhpratt 7,13016 gold badges42 silver badges53 bronze badges asked Nov 24, 2017 at 18:27 Aarón GutiérrezAarón Gutiérrez 1,4304 gold badges19 silver badges41 bronze badges 2- Possible duplicate of stackoverflow.com/questions/17139039/… – Roko C. Buljan Commented Nov 24, 2017 at 18:39
- @RokoC.Buljan No, for this problem I'm not even getting an output, as if the event is not attached to the input... – Aarón Gutiérrez Commented Nov 24, 2017 at 18:41
2 Answers
Reset to default 13keydown
should work, but you could use the input
event which seems to have undesired effects on Android mobile...
To get the code of the pressed key use the jQuery's normalized Event.which
Android Chrome tested:
Using input
Event (e.which
gives always 0
so it seems like a bug on android devices)
jQuery(function($) { // DOM ready and $ alias secured
$('#buscar-producto').on('input', function(e){
var key = e.which || this.value.substr(-1).charCodeAt(0);
alert( key )
});
});
<input type="text" id="buscar-producto" placeholder="Buscar...">
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
Using keydown
(works as expected)
jQuery(function($) { // DOM ready and $ alias secured
$('#buscar-producto').on('keydown', function(e){
alert( e.which );
});
});
<input type="text" id="buscar-producto" placeholder="Buscar...">
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
You can try event.key
, event.keyCode
, event.which
.
If above all not working then you can try event.target.value.splice(-1)