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.

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

Professional jQuery Pro jQuery JavaScript and jQuery: The Missing Manual