Sunday 17 April 2011

Working with jquery key events

The key events are always required to use in application when we dont want to use mouse. the following is the example to handle key events using key codes in jqurery. for all javascript key codes refer http://vikramdoda.wordpress.com/2011/04/10/javascript-key-codes/

Syntax for handle keypress event is
.keypress( [ 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.
<script> var xTriggered = 0;
 $('#target').keypress(function(event) { 
  if (event.which == '13') {   
   event.preventDefault();   
 }  
  xTriggered++;   
 var msg = 'Handler for .keypress() called ' + xTriggered + ' time(s).';  
 $.print(msg, 'html'); 
  $.print(event);
 });
 </script>

No comments:

Post a Comment