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
Parameter | Description |
event | Specifies 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>
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources