jQuery event.which Property
You are Here:
jQuery event.which Property
The jQuery event.which
property returns which keyboard key or mouse button was pressed for the event.
Detecting which Keyboard Key was Pressed
In the following example, we will use keydown()
method along with event.which
property to detect which keyboard key was pressed.
Example
HTML Editor
<!DOCTYPE html>
<html lang="en-US">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>jQuery event.which Property</h1>
<input type="text" placeholder="type something">
<p>keydown: <span id="point"></span></p>
<script>
$(document).ready(function(){
$("input").keydown(function(e){
$("#point").text(e.which);
});
});
</script>
</body>
</html>
Syntax
event.which;
Return Value
Value | Explanation |
---|---|
Number | Returns the Unicode character code of the pressed key. |
Detecting which Mouse Button was Pressed
In the following example, we will use mousedown()
method along with event.which
property to detect which mouse button was pressed.
Example
HTML Editor
<!DOCTYPE html>
<html lang="en-US">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>jQuery event.which Property</h1>
<p>Click on this paragraph any mouse button (left, right, center).</p>
<p>mousedown: <span id="point"></span></p>
<script>
$(document).ready(function(){
$("p").mousedown(function(e){
$("#point").text(e.which);
});
});
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 99.5% of jQuery Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in jQuery.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.