.keypress()S2C Home « Events « .keypress()

Keypress event handler.

Description

The .keypress() method is used to bind an event handler to the JavaScript keypress event or trigger that event on the specified element.

  • The keypress 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 value of 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 the keypress event handler function to determine which key value was passed. Although browsers use different properties to store this information, jQuery normalizes the which property of the event object so you can reliably use it to retrieve the code value of the key that was pressed. This code corresponds to unicode value for each key on the keyboard, excluding codes for special keys.
  • If you want to capture keystrokes for special/modifier keys that do not have a value, use the .keydown() or .keyup() methods instead.

Syntax


Signature Description
.keypress( )Trigger the keypress JavaScript event on the specified element.
.keypress([eventData], handler(eventObject))Bind an event handler to the keypress JavaScript event, optionally passing an object of data.

Parameters


Parameter Description Type
eventDataAn object of data to pass to the event handler.Object
handler( eventObject )A function to execute each time the event is triggered.Function

Return

A jQuery object.

.keypress( ) Examplego to top of page Top

Trigger the keypress JavaScript event on the specified element.

  • This signature is a shortcut for .trigger('keypress').

In the example below we show a new message in the 'div' element with an id of 'div5' every time the radio button for 'beef pie' is clicked on and released.

When the radio button is pushed and released we trigger off the keypress JavaScript event on the 'div3'. This then fires off the $('#div5').keypress(function(){}) code which outputs the message.

Pie Survey

Which pie do you prefer?:

Select a pie shape: 


div5. Some initial text.




$(function(){
  $('#div5').keypress(function () {
    $('#div5').append('<code>keypress</code> JavaScript event triggered.
                       You like Beef Pie!!<br />');
  });
  $('#beef').click(function() {
    $('#div5').keypress();
  });
});

.keypress( [eventData], handler(eventObject) ) Examplego to top of page Top


  • This signature is a shortcut for .bind('keypress', handler).

Bind an event handler to the keypress JavaScript event, optionally passing an object of data.

In the example below we show a new message in the 'p' element with an id of 'scrollspan3' each time the input element with an id of 'input3' below is pressed.

When the input is typed in and the key pressed, the keypress JavaScript event fires off the $('#input3').keypress({ param1: '#scrollspan3', param2: 'keypress', 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: '#scrollspan3', param2: 'keypress', 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(){
  $('#input3').keypress({ param1: '#scrollspan3', param2: 'keypress ', 
                          param3: '**JavaScript event triggered**  ' }, addText2);
  function addText2(event) {
    $(event.data.param1).append(event.data.param2 + ''<code>' + event.data.param3+ ''</code>');
  }
});


We will show a message here.

go to home page Homepage go to top of page Top