Did you find this useful?
Socialize it today.


jQuery Which Event

Monday, June 25, 2012

The jQuery event which property will return, which key or button was pressed for the event. This property can be used on key and mouse events. For mouse button presses, it will report (1) for the left button, (2) for middle button, and (3) for the right button.


Syntax

event.which


ParameterDescription
eventSpecifies the event to check.


Example

In the following jQuery example, the which event can be used to access information about which key or button was pressed for the event. Try by accessing the input field below and type in a few letters using the keyboard, or click on the left, middle, and right mouse buttons. The results will be displayed in the div element next to the mouse icon.


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#input1").keydown(function(e) {
    $("#div1").html(e.type + ": " + e.which);
  });
  $("#input1").mousedown(function(e) {
    $("#div1").html(e.type + ": " + e.which);
  });
});
</script>
</head>
<body>     
<div id="div1">Type in or use your...</div>
</body>
</html>


Type in or use your mouse inside of the input element to the left.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

Professional jQuery Pro jQuery HTML CSS and JavaScript Editor