jQuery keypress | How to use keypress event in jQuery 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.
alert("keypress event occured! The ASCII value of pressed key is: " + event.keyCode);
Enter keyboard keys: <input type="text" id="keyboard">
- How to use jQuery keydown with example
- jQuery keyup | How to use keyup event in jquery with example
- jQuery blur | How to use blur method of jQuery as element loosed focus
- jQuery set focus | How to set focus to html element using jQuery
- 2 Demos of Using jQuery select Method
- Compare jQuery empty and remove Methods with 2 Examples
- jQuery events | How to use mouse click, keyboard, load and other event methods in jquery
- How to use jQuery focus method with a demo