.keyup()

jQuery

.keyup()


.keyup( handler(eventObject) ) Returns: jQuery

Description: Bind an event handler to the "keyup" JavaScript event, or trigger that event on an element.

  • version added: 1.0.keyup( handler(eventObject) )

    • handler(eventObject)
      Type: Function()
      A function to execute each time the event is triggered.
  • version added: 1.4.3.keyup( [eventData ], handler(eventObject) )

    • eventData
      An object containing data that will be passed to the event handler.
    • handler(eventObject)
      Type: Function()
      A function to execute each time the event is triggered.
  • version added: 1.0.keyup()

    • This method does not accept any arguments.

This method is a shortcut for .on('keyup', handler) in the first two variations, and .trigger('keyup') in the third.

The keyup event is sent to an element when the user releases a key on the keyboard. It can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.

For example, consider the HTML:

1
2
3
4
5
6
                                
<form>
<input id="target" type="text" value="Hello there" />
</form>
<div id="other">
Trigger the handler
</div>

The event handler can be bound to the input field:

1
2
3
                                
$('#target').keyup(function() {
alert('Handler for .keyup() called.');
});

Now when the insertion point is inside the field and a key is pressed and released, the alert is displayed:

Handler for .keyup() called.

To trigger the event manually, apply .keyup() without arguments:

1
2
3
                                
$('#other').click(function() {
$('#target').keyup();
});

After this code executes, clicks on Trigger the handler will also alert the message.

If key presses anywhere need to be caught (for example, to implement global shortcut keys on a page), it is useful to attach this behavior to the document object. Because of event bubbling, all key presses will make their way up the DOM to the document object unless explicitly stopped.

To determine which key was pressed, examine the event object that is passed to the handler function. While browsers use differing properties to store this information, jQuery normalizes the .which property so you can reliably use it to retrieve the key code. This code corresponds to a key on the keyboard, including codes for special keys such as arrows. For catching actual text entry, .keypress() may be a better choice.

Example:

Show the event object for the keyup handler (using a simple $.print plugin) when a key is released in the input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
                                  
<!DOCTYPE html>
<html>
<head>
<style>
fieldset { margin-bottom: 1em; }
input { display: block; margin-bottom: .25em; }
#print-output {
width: 100%;
}
.print-output-line {
white-space: pre;
padding: 5px;
font-family: monaco, monospace;
font-size: .7em;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<form>
<fieldset>
<label for="target">Type Something:</label>
<input id="target" type="text" />
</fieldset>
</form>
<button id="other">
Trigger the handler
</button>
<script type="text/javascript" src="/resources/events.js"> </script>
<script>
var xTriggered = 0;
$('#target').keyup(function(event) {
xTriggered++;
var msg = 'Handler for .keyup() called ' + xTriggered + ' time(s).';
$.print(msg, 'html');
$.print(event);
}).keydown(function(event) {
if (event.which == 13) {
event.preventDefault();
}
});
$('#other').click(function() {
$('#target').keyup();
});</script>
</body>
</html>