jQuery mouseleave() Method

Last Updated:

jQuery mouseleave() Method

The jQuery mouseleave() method is used for the following 2 purpose

  1. To run a callback when a mouse left a specified element.
  2. To trigger the mouseleave event of a specified element.

Run a Callback

In the following example, we will run a callback function when a mouse left a specified element.

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 mouseleave() Method</h1> <p>Try to touch me and leave.</p> <script> $(document).ready(function(){ $("p").mouseleave(function(){ alert("You are go away from paragraph."); }); }); </script> </body> </html>

Syntax

$(selector).mouseleave(callback);

Parameter Values

ValueTypeExplanation
callback Optional Specifies a function to be called once user leave the element.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
mouseleave()YesYesYesYesYesYesYesYesYesYesYesYesYes

Trigger a mouseleave Event

In the following example, we will trigger a mouseleave event when a button is clicked.

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 mouseleave() Method</h1> <p>Try to touch me and leave.</p> <button>Trigger mouseleave Event</button> <script> $(document).ready(function(){ $("p").mouseleave(function(){ alert("You are go away from paragraph."); }); $("button").click(function(){ $("p").mouseleave(); }); }); </script> </body> </html>

mouseleave() vs mouseout()

Check out the difference between mouseleave() vs mouseout() in jquery.

Share this Page

Meet the Author