jQuery Mouse Events

This article will explain different mouse events occurring based on mouse positions on a particular HTML element.

Mouse Events in jQuery:

  • mouseenter and mouseleave
  • mouseup and mousedown
  • mouseover and mouseout

mouseenter and mouseleave: The mouseenter event occurs when the mouse is placed over the HTML element and mouseleave event occurs when the mouse is removed from the element.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script src="jquery.js"></script>
</head>
  
<body bgcolor="cyan">
    <p id="key">Original Text</p>
  
  
    <script>
        $("document").ready(function () {
            $("#key").mouseenter(enter);
            $("#key").mouseleave(leave);
            function enter() {
                $("#key").text(
                    "mouseenter event has occured");
            }
            function leave() {
                $("#key").text(
                    "mouseleave event has occured");
            }
        });
    </script>
</body>
  
</html>

chevron_right


Output:



  • On loading the webpage:
  • MouseEnter and MouseLeave events:
  • MouseUp and MouseDown events:

    mouseup and mousedown requires a mouse-click to occur.

    Javascript

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
        <body bgcolor="#ff00ff">
            <p id="key">Original Text</p>
        </body>
            <script src = "jquery.js"></script>
            <script>
              $("document").ready(function()
               
                  $("#key").mouseup(up);
                  $("#key").mousedown(down);
                  function up()
                   {
                     $("#key").text("mouseup event has occured");
                   }
                  function down()
                   {
                     $("#key").text("mousedown event has occured");
                   }
              });
            </script>
    </html>

    chevron_right

    
    

    Output:

    • On landing web page:
    • MouseUp and MouseDown events:
  • Mouseover and Mouseout:

    These events occur when the mouse is placed over some specific HTML element.

    Javascript

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>   
        <body bgcolor="#87FF2A">
            <p id="key">Original Text</p>
        </body>
        <script src = "jquery.js"></script>
            <script>
                $("document").ready(function()
               
                  $("#key").mouseover(over);
                  $("#key").mouseout(out);
                   function over()
                    {
                      $("#key").text("mouseover event has occured");
                    }
                   function out()
                    {
                      $("#key").text("mouseout event has occured");
                    }
              });
            </script>   
    </html>

    chevron_right

    
    

    Output:

    • Onloading web page:
    • MouseOver and MouseOut events:




  • My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.