Trigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?

For this, use the addEventListener() with mousedown event.


Following is the code −

<!DOCTYPE html>
<html lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<link href="" rel="stylesheet" />
   document.addEventListener("mousedown", function () {
      console.log("Mouse down event is happening");

To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with Live Server” in VS Code editor.


This will produce the following output on console −

When you click the mouse, the event will generate. The console output is shown below −

Updated on: 09-Nov-2020


Kickstart Your Career

Get certified by completing the course

Get Started