Detecting key press events on inputs
jQuery provides three event functions that allow the jQuery developer to determine what key a user is pressing, and when and how the user is pressing it. The .keyup()
function is an event handler that can be attached to an input and will be fired once the pressed key has been fully released; likewise, .keydown()
will be fired once the key has been fully pressed. The third available event handler is .keypress()
, which is fired instantly when a key is pressed.
These methods allow the developer to provide powerful client-side validation or to provide the user with simple features such as triggering a form submission when the Enter key is pressed.
Getting ready
Create a blank HTML file named recipe-5.html
which we can use for this recipe.
How to do it…
Use a variety of event handlers to detect user key press events by performing the following steps:
Add the following HTML code to the web page you have just created. Update the reference to the jQuery library to ensure...