← Back to jQuery Course | Chapter 3: Events | Lesson 2 of 11

Mouse Events

The click event runs when the user clicks an element.

Click

Binding a handler to the click event runs your function the instant the user presses and releases the mouse button over that element.

Example: Click

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Click</button>
    <script>
      $("#btn").on("click", function() {
      console.log("Clicked");
      });
    </script>
  </body>
</html>

Double Click

The dblclick event only fires after two clicks register in quick succession -- a single click alone won't trigger it, which makes it useful for actions you want to guard against accidental single clicks.

Example: Double Click

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Double-click</button>
    <script>
      $("#btn").on("dblclick", function() {
      console.log("Double clicked");
      });
    </script>
  </body>
</html>

Mouse Down and Up

mousedown fires the moment the button is pressed, and mouseup fires when it's released -- splitting the click into two separate events lets you build custom drag or press-and-hold interactions.

Example: Mouse Down and Up

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Press and hold</button>
    <script>
      $("#btn").on("mousedown", function() {
      console.log("Button pressed");
      });
      $("#btn").on("mouseup", function() {
      console.log("Button released");
      });
    </script>
  </body>
</html>

Mouse Enter and Leave

mouseenter fires once when the pointer crosses into an element's boundary, and mouseleave fires once when it exits -- unlike mouseover/mouseout, they don't re-fire for child elements inside.

Example: Mouse Enter and Leave

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Hover me</div>
    <script>
      $("#box").on("mouseenter", function() {
      console.log("Entered");
      });
      $("#box").on("mouseleave", function() {
      console.log("Left");
      });
    </script>
  </body>
</html>

Mouse Move

mousemove fires repeatedly, many times per second, for as long as the pointer stays over the element -- useful for tracking cursor position but expensive if the handler does heavy work.

Example: Mouse Move

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="box">Move mouse here</div>
    <script>
      $("#box").on("mousemove", function(event) {
      console.log(event.pageX, event.pageY);
      });
    </script>
  </body>
</html>

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.