event.which JQ Home  <<  Objects  <<  event.which

Event key/button pressed information for keyboard/mouse events property.

Description

The event.which Event object property, contains a value indicating the key or button that was pressed for keyboard and mouse events.

  • The event.which property normalizes event.keyCode and event.charCode values across browsers.
  • The event.which property also normalizes the the mousedown and mouseup JavaScript events values across browsers, reporting 1 for left button presses, 2 for middle button presses, and 3 for right button presses.

Syntax

Signature Description
event.whichA value indicating the key or button that was pressed for keyboard and mouse events..

Parameters

None.

Return

A Number object.

event.which Example Objects  <<  Top

Contains a value indicating the key or button that was pressed for keyboard and mouse events.

When we click the mouse button in the div below with an id of 'div7' a message is output.


$(function(){
  $('#div7').on('click', function(event) {
    if ( event.which == 1 ) {
       $('#div7').append('You pressed the left mouse button.
'); } }); });

div7. Some initial text.

Related Tutorials

jQuery Advanced Tutorials - Lesson 5 - The Event Object