jQuery API

.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)A function to execute each time the event is triggered.

  • version added: 1.4.3.keyup( [eventData], handler(eventObject) )

    eventDataA map of data that will be passed to the event handler.

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.0.keyup()

This method is a shortcut for .bind('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:

<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:

$('#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:

$('#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.

<!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="/scripts/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>

Demo:

Support and Contributions

Need help with .keyup() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .keyup()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • zofris

    Using this function generates a Firebug warning using Firefox 3.5.8 and Firebug 1.5.2

    The error:
    “The ‘charCode’ property of a keyup event should not be used. The value is meaningless.”

  • Ryan Maloney

    When using keyup and and keypress events on the same text field causes strange behavior.
    1. when you press a key like 'ctrl' the keypress event fires as natural. keyup doesn't fire as expected.
    2. when you press a key like 'a' the keypress event seems to be firing 2x and the keyup is never fired. However, this only happens if you hit a non-character key 1st like the 'ctrl' key.
    3. if you press the 'a' key before hitting 'ctrl' the keypress event will fire 1x but the keyup will not fire at all. After this, if you hit a non-char key such as 'ctrl' and then press a char key the keypress event fires 1x and the keyup never fires. I've tried chaining these events in different order with similar results.

  • http://twitter.com/trevorrowe trevorrowe

    I’m getting the exact same issue using Firefox 3.6 and Firebug 1.5.2, it doesn’t seem to affect usability, just spams the error log like crazy.

  • Ryan Maloney

    ** it looks like this behavior only happens if i use alert(‘something’) in the event handlers. using console.log in firebug appears to work correctly.

  • bukko

    I can’t get keyup to recognise the tab key.
    I originally thought it was because the focus was moving from the control with the event code after tab was pressed (the event is only sent to the element that has the focus) but also binding the event to the document or body also doesn’t work!
    In which case, how is it possible to detect the tab key??

  • Ontananza

    Just pass the event javascript variable:

    $(“input[name*=_costo_]“).keyup(function(event){
    //-tab keyCode
    if(event.keyCode!='9')
    numero_e(this,event,0);
    });

  • http://dustin-burke.com/ burkestar

    keydown or keypress work for tab key

  • http://aaronmueller.myopenid.com/ Aaron
  • http://pulse.yahoo.com/_XUSAPGK4ZEU6KWG4NACF2WY7CI Joshua

    Ditto for Enter or Return key — need to use keydown or keypress
    if (event.keyCode == '13') { event.preventDefault(); }

    is ignored in keyup, and in this case, the frame reloads the whole page.

  • Dave

    I just go into the jQuery minimised source and change the charCode property to XcharCode and all is fine.

  • Dave

    I just go into the jQuery minimised source and change the charCode property to XcharCode and all is fine.

  • http://twitter.com/asdLArs Larry

    it fails to recognize key up on the following characters: ~¨`´^

  • http://twitter.com/asdLArs Larry

    *edited: double post, had trouble with disqus login.