jQuery event.which

Beginner
5 min read
Updated: Oct 2026
5 examples
Live preview

What Is event.which?

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.

Remember
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)
NeedUse
Which key / button (jQuery)event.which
Event name stringevent.type
Meta / Command held?event.metaKey
Bind keydown.on("keydown", fn)
Bind handlers.on()

How to Use event.which

Read it on the event object inside a keyboard or mouse handler.

  1. Bind keydown (keys) or mousedown / mouseup (buttons).
  2. Accept the event argument in the handler.
  3. Read event.which (property — no ()).
  4. Compare to a known code (13 Enter, 1 left button, …).
  5. Optionally show event.type + ": " + event.which while learning.
PieceRequired?Role
eventYesjQuery event object in the handler
which—Number: key code or mouse button
ArgumentsN/AProperty — not a function
typeOptionalEvent name next to the number

Pseudocode

Pseudocode
input.on("keydown", handler)
box.on("mousedown", handler)

when keydown fires:
    which = normalized key code
    log type + ": " + which

when mousedown fires:
    which = 1 (left) or 2 (middle) or 3 (right)
    log type + ": " + which

// Common key checks:
if which === 13 → Enter
if which === 27 → Escape

Cheat Sheet

Keep these codes handy while reading which.

TopicDetail
TypeNumber property (not a method)
SincejQuery 1.1.3
KeyboardNormalizes keyCode / charCode
Mouse1 left · 2 middle · 3 right
Enter / Escape13 / 27
Prefer overevent.keyCode, event.button (in jQuery)
Official demoskeydown + mousedown log type: which
Official docsapi.jquery.com/event.which/

Live Preview

Focus the input and press keys — same idea as the official jQuery API keydown demo. Or press a mouse button on the box below.

Demo

Logs event.type + ": " + event.which.

Log
Waiting — type or click.

jQuery Examples

Short, copy-ready snippets. Open DevTools, or use Try It Yourself for a live editor.

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.
  4. Normalize. jQuery merges keyCode / charCode into one Number.

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.
  4. Prefer which. jQuery recommends it over event.button.

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.
  4. Scope. Bind on the field that should listen for Enter.

Example 4 — Detect Escape (27)

Clear a message when Escape is pressed.

jQuery
$("#panel").on("keydown", function (event) {
  if (event.which === 27) {
    $("#out").text("Escape — panel closed.");
  }
});
Try It Yourself

How It Works

  1. Compare. event.which === 27 means Escape.
  2. Focus. The element must be focusable to receive keydown.
  3. UX. Common pattern for closing dialogs or clearing UI.
  4. Keep it lean. One check, one clear action.

Example 5 — Map Mouse which to Names

Turn 1 / 2 / 3 into readable labels.

jQuery
$("#box").on("mousedown", function (event) {
  var name = { 1: "left", 2: "middle", 3: "right" }[event.which]
    || "unknown";
  $("#out").text(event.type + ": " + event.which + " (" + name + ")");
});
Try It Yourself

How It Works

  1. Read which. Get the normalized button Number.
  2. Map. Look up a friendly name from 1–3.
  3. Log. Show type, number, and name together.
  4. Fallback. Use unknown for unexpected values.

Edge Cases & Pitfalls

Watch for these when using event.which.

property

Not a method

Write event.which, not event.which().

event type

Meaning depends on the event

On keys it is a key code; on mouse down/up it is a button (1–3).

click

Prefer mousedown for buttons

Official mouse demo uses mousedown / mouseup, not click.

modern

event.key is clearer for new code

Strings like "Enter" avoid magic numbers — use them outside legacy jQuery patterns when you can.

Browser Support

event.which is a jQuery event-object property available since 1.1.3. It normalizes key and mouse button values across browsers. Works wherever your loaded jQuery version runs.

✓ Stable · jQuery 1.1.3+

event.which

Supported in jQuery 1.1.3–3.x. Prefer jQuery 3.x with .on() handlers. Use keydown for keys and mousedown/mouseup for buttons 1/2/3.

100% With jQuery 1.1.3+
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
which jQuery 1.1.3+

Bottom line: Safe for jQuery codebases. For new non-jQuery code, prefer event.key (and modern mouse button APIs) over raw which.

Performance Notes

ApproachCostBest for
Read event.whichFreeOne Number check in a handler
Compare to 13 / 27FreeEnter / Escape shortcuts
Document-level keydownCheapGlobal shortcuts; keep the handler tiny

Reading which is free. Cost comes from work you do after the compare — keep shortcut handlers short.

Key Takeaways

  • Role: Number — which key or mouse button fired.
  • Official: log event.type + ": " + event.which.
  • Mouse: 1 left · 2 middle · 3 right.
  • Keys: 13 Enter · 27 Escape.

One line: event.which is jQuery’s normalized Number for “which key or button?”

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

Read the namespace from namespaced binds like click.myPlugin.

event.namespace tutorial →

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