Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to run code on mouseleave event in jQuery ?

  • Last Updated : 30 Sep, 2021

In this article, we will see how to run the code when the mouse leaves into a specific area using jQuery. To run the code on a mouse leave into a specific area, the mouseleave() method is used. The mouseleave() method works when the mouse pointer leaves the selected element.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

$(selector).mouseleave(function)

Parameters: This method accepts a single parameter function which is optional. It is used to specify the function to run when the mouseleave event is called.



 

Example:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to run code on mouseleave 
        event in jQuery ?
    </title>
  
    <script src=
    </script>
  
    <script>
        $(document).ready(function() {
            $(".main").mouseleave(function() {
                $(".main").css({
                    background: "green",
                    color: "white"
                });
            });
            $(".main").mouseenter(function() {
                $(".main").css({
                    background: "none",
                    color: "black"
                });
            });
        });
    </script>
  
    <style>
        .main {
            width: 300px;
            height: 200px;
            border: 2px solid black;
        }
    </style>
</head>
  
<body>
    <center>
        <div class="main">
            <h1>GeeksforGeeks</h1>
  
            <h3>
                How to run code on mouseleave 
                event in jQuery ?
            </h3>
        </div>
    </center>
</body>
  
</html>

Output: 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!