- jQuery 2.0 Development Cookbook
- Leon Revill
- 515字
- 2021-07-16 12:25:32
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 that the latest version is being referenced into the web page. This HTML creates a simple page that has an input and an unordered list element, which we can use to output some event information in order to illustrate what each part of our jQuery code is achieving.
<!DOCTYPE html> <html> <head> <title>Chapter 2 :: jQuery Events</title> <script src="jquery.min.js"></script> <script> </script> </head> <body> <input type="text" class="myInput" /> <ul id="myList"></ul> </body> </html>
- Within the script tags, add the following JavaScript code to attach both the
keyup
andkeydown
event handlers:$(function(){ $('.myInput').keyup(function(){ $('#myList').append("<li>Key up!</li>"); }); $('.myInput').keydown(function(event){ $('#myList').append("<li>Key down!</li>"); if (event.which == 13) { $('#myList').append("<li>Enter key pressed!</li>"); } }); });
How it works…
We can attach both the keyup
and keydown
event handlers by first selecting our .myInput
element and then specifying one of the event handler functions as shown in the following code:
$('.myInput').keydown();
We have added an event
variable as an argument to the callback function on the keydown
event. From this event
variable, we can detect which key has been pressed using event.which
. This is often useful as we can determine whether the key that the user has just pressed down is the Enter key, which we would be likely to want to perform a specific action on, such as for form submission or when we want to trigger an AJAX call. In this example, we simply append a list item to our #myList
unordered list to illustrate the concept.
We replicate this procedure within our keyup
event handler and use the .append()
function to append a new DOM element into the list.
Once you load this web page in a browser and enter text in the input box, you will be able to see the events trigger as the list element updates on every key press. You will be able to see something similar to the following screenshot:

There's more…
This recipe provides two examples with keydown
and keyup
. Try experimenting with the code, and use the alternative keypress()
function in the same way to see how it works.
See also
- Detecting button clicks
- Detecting element clicks
- Restricting input character length
- Detecting change
- Google Apps Script for Beginners
- Linux C/C++服務(wù)器開(kāi)發(fā)實(shí)踐
- Java Web程序設(shè)計(jì)
- Java設(shè)計(jì)模式及實(shí)踐
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- C/C++數(shù)據(jù)結(jié)構(gòu)與算法速學(xué)速用大辭典
- Unity 2018 Augmented Reality Projects
- Python從入門到精通(第3版)
- Java Web從入門到精通(第2版)
- Java 從入門到項(xiàng)目實(shí)踐(超值版)
- Mastering Apache Camel
- 零基礎(chǔ)學(xué)編程系列(全5冊(cè))
- HikariCP數(shù)據(jù)庫(kù)連接池實(shí)戰(zhàn)
- 零基礎(chǔ)學(xué)Java(第5版)
- Kubernetes進(jìn)階實(shí)戰(zhàn)(第2版)