jQuery keypress | How to use keypress event with example

jQuery keypress | How to use keypress event with example

jQuery keypress event

The keypress event is triggered after a key is pressed on the keyboard. The keypress() method will be used to perform any action or run a function as keypress event occurs in your web document.

Keypress event occurs when a key is pressed down, so it is quite similar to keydown event. However, there are differences between the two – for example pressing a few keys will trigger only keydown event like ALT, CTRL, ESC etc.

Also, the keypress event returns different values for lowercase and uppercase letters, whereas keyup and keydown return same. So it’s useful to use keypress event in case you have to differentiate between lowercase and uppercase.

This can be useful for many scenarios, for example, in passwords boxes only allowing letters, numbers and a few special characters while notifying users if a certain key is not allowed. In that case, rather than sending the request to a server and then checking input by visitor one can check it at the client side.

Syntax of keypress event


Where function is optional, to execute as keypress event occurs.

An example of keypress event

The example below shows when a key is pressed, and keypress event occurs, the keypress() method will capture the event and throw an alert with ASCII value of the pressed key.

jQuery keypress

Experience this example online

Related : keydown() event | keyup() event