jQuery event.which

Beginner
5 min read
Updated: Nov 2026
3 examples
Events

Definition

A Number property that tells you which key or which mouse button was pressed. Added in jQuery 1.1.3. It normalizes keyCode / charCode for keys, and reports 1 / 2 / 3 for left / middle / right mouse buttons.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
event.which  // Number property — no parentheses

// Official keydown idea
$("#whichkey").on("keydown", function (event) {
  $("#log").html(event.type + ": " + event.which);
});

// Mouse: 1 = left, 2 = middle, 3 = right
// Keys: 13 = Enter, 27 = Escape (common codes)
CallWhat it does
event.whichLearn jQuery event.which: read the normalized key or mouse button as a Number, official keydown and mousedown demos, Enter and Escape shortcuts, 1/2/3 for left/middle/right, examples, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Bind keydown (keys) or mousedown / mouseup (buttons).
Step 2Accept the event argument in the handler.
Step 3Read event.which (property — no ()).

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official: Log Key on keydown

Show event.type and event.which for each key.

jQuery
$("#whichkey").on("keydown", function (event) {
  $("#log").html(event.type + ": " + event.which);
});
Try It Yourself

How It Works

  1. Bind. Attach keydown to the input.
  2. Read. Use event.which — no parentheses.
  3. Log. Combine with event.type for clarity.

Example 2 — Official: Log Mouse Button

On mousedown, which is 1, 2, or 3.

jQuery
$("#whichkey").on("mousedown", function (event) {
  $("#log").html(event.type + ": " + event.which);
});
Try It Yourself

How It Works

  1. Bind. Use mousedown (or mouseup).
  2. Left. which === 1.
  3. Middle / right. 2 and 3.

Example 3 — Detect Enter (13)

Run an action when the user presses Enter in an input.

jQuery
$("#name").on("keydown", function (event) {
  if (event.which === 13) {
    event.preventDefault();
    $("#out").text("Enter pressed — value: " + $(this).val());
  }
});
Try It Yourself

How It Works

  1. Compare. event.which === 13 means Enter.
  2. Prevent default. Stop unwanted form submit if needed.
  3. Act. Read the input value and show a message.

Frequently Asked Questions

event.which is a Number property on the jQuery event object. For keyboard events it indicates which key was pressed; for mousedown and mouseup it indicates which button was pressed. Available since jQuery 1.1.3.
Bind keydown: $("#whichkey").on("keydown", function(event) { $("#log").html(event.type + ": " + event.which); }); The log shows the event type and numeric code. The mousedown variant logs 1, 2, or 3 for left, middle, and right.
jQuery recommends event.which for keyboard input. It normalizes event.keyCode and event.charCode into one Number. Prefer which over raw keyCode in jQuery handlers.
On mousedown and mouseup, event.which reports 1 for left, 2 for middle, and 3 for right. Use event.which instead of event.button — jQuery normalizes browser differences.
Compare event.which in a keydown handler: 13 for Enter, 27 for Escape. Bind on the input or container and call preventDefault when you handle the key yourself.
In the modern DOM, KeyboardEvent.which is deprecated in favor of event.key (and mouse buttons favor event.buttons / event.button). jQuery still documents event.which as the normalized Number for key and mouse in jQuery code. Prefer event.key for new non-jQuery code when you can.

Next: event.namespace Property

Read the namespace from namespaced binds like click.myPlugin.

event.namespace Property tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful