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>

<inputid="target"type="text"value="Hello there">

</form>

<divid="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.

Additional Notes:

As the .keyup() method is just a shorthand for .on( "keyup", handler ), detaching is possible using .off( "keyup" ).

Example:

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