.keyup()
JQ Home <<
Events <<
.keyup()
Keyup event handler.
Description
The .keyup()
method is used to bind an event handler to The JavaScript keydown
event or trigger that event on the specified element.
- The
keyup
event is sent to an element when the user releases a key on the keyboard and can be attached to any element:- The event is only sent to the element that has focus.
- Form elements are a good candidate for this event as they can always get focus, unlike other focusable elements which can vary, dependant upon the browser.
- If it is a requirement to capture the key that was pressed, attach this behavior to the
document
object. All key presses will make their way up the DOM to the document object, due to event bubbling unless explicitly stopped within the code:- You can examine the
event
object by passing it to thekeyup
event handler function to determine which key was pressed. Although browsers use different properties to store this information, jQuery normalizes thewhich
property of theevent
object 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.
- You can examine the
- If you want to capture the text entry from the user, use the
.keypress()
method instead.
Syntax
Signature | Description |
---|---|
.keyup( ) | Trigger the keyup JavaScript event on the specified element. |
.keyup( handler(eventObject) ) | Bind an event handler to the keyup JavaScript event. |
.keyup( [eventData ,] handler(eventObject) ) | Bind an event handler to the keyup JavaScript event, optionally passing an object of data. |
Parameters
Parameter | Description | Type |
---|---|---|
handler( eventObject ) | A function to execute each time the event is triggered. | Function |
eventData | An object of data to pass to the event handler. | Anything |
Return
A jQuery
object.
.keyup( )
Example
Events << Top
Trigger the keyup
JavaScript event on the specified element.
- This signature is a shortcut for
.trigger('keyup')
.
In the example below we show a new message in the 'div' element with an id of 'div3' every time the radio button for 'fish pie' is clicked on and released.
When the radio button is pushed and released we trigger off the keyup
JavaScript event on the 'div1'. This then fires off the $('#div1').keyup(function(){})
code which outputs the message.
div1. Some initial text.
$(function(){
$('#div1').keyup(function () {
$('#div1').append('<code>keyup</code> JavaScript event triggered.
You like Fish Pie!!<br>');
});
$('#fish') .click(function() {
$('#div1').keyup();
});
});
.keyup( handler(eventObject) )
Example
Events << Top
Bind an event handler to the keyup
JavaScript event.
- This signature is a shortcut for
.on('keyup', handler)
.
In the example below we show a new message in the 'p' element with an id of 'scrollspan1' each time the input element with an id of 'input1' below is typed in.
When the input is typed in, the keyup
JavaScript event fires off the addText(event)
mothod which outputs a message.
What we are doing here is passing across the event
object to the function addText(event)
method. The data we specify gets tagged onto the event.data
property.
$(function(){
$('#input1').keyup(addText);
function addText(event) {
$('#scrollspan1').append('keyup 1 **JavaScript event triggered**<br>');
}
});
We will show a message here.
.keyup( [eventData ,] handler(eventObject) )
Example
Events << Top
Bind an event handler to the keyup
JavaScript event, optionally passing an object of data.
- This signature is a shortcut for
.bind('keyup', handler)
.
In the example below we show a new message in the 'p' element with an id of 'scrollspan2' each time the input element with an id of 'input2' below is typed in and the key is released.
When the input is typed in and the key released, the keyup
JavaScript event fires off the $('#input2').keyup({ param1: '#scrollspan2', param2: 'keyup', param3: '**JavaScript event triggered** ' }, addText2);
code.
What we are doing here is passing across the event
object to the function addText2(event)
. The map we specify, in our case { param1: '#scrollspan2', param2: 'keyup', param3: '**JavaScript event triggered** ' }
gets tagged onto the event.data
property. We then access this parameter in the function via event.data.param
and use it as part of the appended data.
$(function(){
$('#input2').keyup({ param1: '#scrollspan2', param2: 'keyup 2 ',
param3: '**JavaScript event triggered**<br>' }, addText2);
function addText2(event) {
$(event.data.param1).append( event.data.param2 + event.data.param3);
}
});
We will show a message here.
Related Tutorials
jQuery Advanced Tutorials - Lesson 2 - Keyboard & Mouse Events