HTML | DOM MouseEvent

Events that occur when the mouse interacts with the HTML document falls under the category of MouseEvent property.

Event Types:

  • onclick: The event occurs when the user clicks on an element.
  • onmousedown: The event occurs when the button pressed over an element.
  • onmouseenter: The event occurs when the pointer is moved onto an element.
  • onmouseout: The event occurs when a user moves the mouse pointer out of an element, or out of one of its children.
  • onmouseup: The event occurs when a user releases a mouse button over an element.
  • oncontextmenu: This event occurs on right-click on an element.
  • ondblclick: This event occurs on double-click on an element.
  • onmouseleave: When pointer is moved out of an element.
  • onmousemove: This event occurs while pointer move over element.
  • onmouseover: This event occurs when the pointer move over an element, or its children.

Syntax:



  • onmouseup property:
    <element onmouseup="ID">
  • onmousedown property:
    <element onmousedown="ID">
  • onclick property:
    <element onclick="ID">
  • oncontextmenu property:
    <element oncontextmenu="ID">
  • ondblclick property:
    <element ondblclick="ID">
  • onmouseenter property:
    <element onmouseenter="ID">
  • onmouseleave property:
    <element onmouseleave="ID">
  • onmousemove property:
    <element onmousemove="ID">
  • onmouseout property:
    <element onmouseout="ID">
  • onmouseover property:
    <element onmouseover="ID">

Example-1: Below program illustrate onmousedown and onmouseup property.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
  
  <center>
  <h3>GEEKS FOR GEEKS</h3>
    <p id="myP" onmousedown="mouseDown()" onmouseup="mouseUp()">
        . The mouseDown() function works when the mouse button 
           is pressed down over this paragraph and sets the color of the text to 'Yellow'. 
          The mouseUp() function works when the mouse button is 
            released and sets the color of the text to 'Red'.
    </p>
  
    <script>
        function onmouseDown() {
            
            //  using onmousedown property
            document.getElementById("myP").style.color = "yellow"; 
        }
  
        function mouseUp() {
          
            //  using onmouseup property
            document.getElementById("myP").style.color = "red"; 
        }
    </script>
      
  </center>
</body>
  
</html>

chevron_right


Output:
Initial:

mouseDown:

mouseUp:

Supported Browsers:

  • Google Chrome
  • Mozilla Firefox
  • Edge
  • Opera
  • Safari


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.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


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