What is the mouseleave() method in jQuery?

Overview

The mouseleave() method triggers the mouseleave event when the mouse pointer leaves a selected element. It is a short-hand of the on('mouseleave', eventHandler) event.

Syntax

$(selector).mouseleave(function());

Parameters

This method accepts only one parameter, a function that gets invoked when the mouseleave() method triggers.

Example

Let’s look at the code below:

Explanation

In the HTML tab:

  • Line 6: We create a div content.
  • Line 7: We import the jQuery script.

In the JavaScript tab:

  • Line 4: We attach the mouseleave() method on the div content.
  • Line 6: We use the css() method to change the background color of the div content when mouseleave() is invoked.
  • Lines 10 to 15: We create a function that generates a random color and returns it.

Output

  • After we enter the square, the mouseleave() method is invoked when the mouse pointer leaves the square. As a result, the background color of the square changes.

Free Resources