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.



eventSpecifies the event to check.


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>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
  $("#input1").keydown(function(e) {
    $("#div1").html(e.type + ": " + e.which);
  $("#input1").mousedown(function(e) {
    $("#div1").html(e.type + ": " + e.which);
<div id="div1">Type in or use your...</div>

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

JavaScript and jQuery: The Missing Manual HTML CSS and JavaScript Editor Pro jQuery