.keydown()
S2C Home « Events « .keydown()
Keydown event handler.
Description
The .keydown()
method is used to bind an event handler to the JavaScript keydown
event or trigger that event on the specified element.
- The
keydown
event is sent to an element when the user presses 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 thekeydown
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 |
---|---|
.keydown( ) | Trigger the keydown JavaScript event on the specified element. |
.keydown( handler(eventObject) ) | Bind an event handler to the keydown JavaScript event. |
.keydown( [eventData ,] handler(eventObject) ) | Bind an event handler to the keydown 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.
.keydown( )
Example Top
Trigger the keydown
JavaScript event on the specified element.
- This signature is a shortcut for
.trigger('keydown')
.
In the example below we show a new message in the 'div' element with an id of 'div1' every time the radio button for 'chicken pie' is clicked on.
When the radio button is pushed we trigger off the keydown
JavaScript event on the 'div1'. This then fires off the $('#div1').keydown(function(){})
code which outputs the message.
div1. Some initial text.
$(function(){
$('#div1').keydown(function () {
$('#div1').append('<code>keydown</code> JavaScript event triggered.
You like Chicken Pie!!<br />');
});
$('#chicken').click(function() {
$('#div1').keydown();
});
});
.keydown( handler(eventObject) )
Example Top
Bind an event handler to the keydown
JavaScript event.
- This signature is a shortcut for
.on('keydown', 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 keydown
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').keydown(addText);
function addText(event) {
$('#scrollspan1').append('keydown 1 **JavaScript event triggered**<br>');
}
});
We will show a message here.
.keydown( [eventData ,] handler(eventObject) )
Example Top
Bind an event handler to the keydown
JavaScript event, optionally passing an object of data.
- This signature is a shortcut for
.on('keydown', 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 keydown
JavaScript event fires off the $('#input2').keydown({ param1: '#scrollspan2', param2: 'keydown 2', 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: '#scrollspan1', param2: 'keydown', 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').keydown({ param1: '#scrollspan2', param2: 'keydown 2 ',
param3: '**JavaScript event triggered** ' }, 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