Mouse Events

The complete set of mouse-related events:

NameDescription
clickTriggered when the mouse button is clicked and released.
dblclickTriggered when the mouse button is clicked and released twice.
mousedownTriggered when the mouse button is clicked.
mouseenterTriggered when the pointer is moved to be within the screen region occupied by the element or one of its descendants.
mouseleaveTriggered when the pointer is moved to be outside the screen region occupied by the element and all its descendants.
mousemoveTriggered when the pointer is moved while over the element.
mouseoutThis is the same as for mouseleave, except that this event will trigger while the pointer is still over a descendant element.
mouseoverThis is the same as for mouseenter, except that this event will trigger while the pointer is still over a descendant element.
mouseupTriggered when the mouse button is released.

When a mouse event is triggered, the browser dispatches a MouseEvent object.

The MouseEvent Object

NameDescriptionReturns
buttonwhich button has been clicked; 0 is the main mouse button, 1 is the middle button, and 2 is the secondary/right button.number
altKeyif the alt/option key was clicked.boolean
clientXReturns the X position of the mouse.number
clientYReturns the Y position of the mouse.number
screenXReturns the X position of the mouse, relative to the screen coordinate system.number
screenYReturns the Y position of the mouse, relative to the screen coordinate system.number
shiftKeyReturns true if the Shift key was pressed.boolean
ctrlKeyReturns true if the Ctrl key was pressed.boolean
 
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style type="text/css">
p {
  background: gray;
  color: white;
  padding: 10px;
  margin: 5px;
  border: thin solid black
}

</style>
</head>
<body>
  <p id="block1">Move the mouse here.</p>
  <table>
    <tr>
      <th>Type:</th>
      <td id="eType"></td>
    </tr>
    <tr>
      <th>X:</th>
      <td id="eX"></td>
    </tr>
    <tr>
      <th>Y:</th>
      <td id="eY"></td>
    </tr>
  </table>
  <script type="text/javascript">
    var textblock = document.getElementById("block1");
    var typeCell = document.getElementById("eType");
    var xCell = document.getElementById("eX");
    var yCell = document.getElementById("eY");
    textblock.addEventListener("mouseover", handleMouseEvent, false);
    textblock.addEventListener("mouseout", handleMouseEvent, false);
    textblock.addEventListener("mousemove", handleMouseEvent, false);
    function handleMouseEvent(e) {
      if (e.eventPhase == Event.AT_TARGET) {
        typeCell.innerHTML = e.type;
        xCell.innerHTML = e.clientX;
        yCell.innerHTML = e.clientY;
        if (e.type == "mouseover") {
          e.target.style.background = 'black';
          e.target.style.color = 'white';
        } else {
          e.target.style.removeProperty('color');
          e.target.style.removeProperty('background');
        }
      }
    }
  </script>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    DOM