Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript keypress event not raised on Android browser

I have created a simple code to handle keypress event:

var counter = 0;         $('input').on('keypress', function () {     $('div').text('key pressed ' + ++counter); }); 

JSFiddle.

But keypress event handler is not raised on mobile browser (Android 4+, WindowsPhone 7.5+). What could be the issue?

like image 510
alex.mironov Avatar asked Jan 10 '14 22:01

alex.mironov


People also ask

Does Keydown work on mobile?

It works on computers, but not on mobile..

What is the difference between jquery's change () and keypress () events?

The change event occurs if the value has been changed when the element loses focus. The keypress event occurs every time you press down and release a (non control) key.

What is KeyboardEvent?

KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard. The event type ( keydown , keypress , or keyup ) identifies what kind of keyboard activity occurred.

What does keypress do in Javascript?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .


2 Answers

I believe keypress is deprecated now. You can check in the Dom Level 3 Spec. Using keydown or keyup should work. The spec also recommends that you should use beforeinput instead of keypress but I'm not sure what the support of this is.

like image 51
Adam Hughes Avatar answered Sep 24 '22 13:09

Adam Hughes


Use the keyup event:

// JavaScript: var counter = 0;         document.querySelector('input').addEventListener('keyup', function () {     document.querySelector('div').textContent = `key up ${++counter}`; });  // jQuery: var counter = 0;         $('input').on('keyup', function () {     $('div').text('key up ' + ++counter); }); 
like image 45
HDT Avatar answered Sep 22 '22 13:09

HDT