Javascript DOM onmousemove Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("mousemove",
       myScript);

This example uses the addEventListener() method to attach a "mousemove" event to a div element.

Mouse over the rectangle, and get the coordinates of your mouse pointer.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//from www  .j a va 2 s. com
  width: 200px;
  height: 100px;
  border: 1px solid black;
}
</style>
</head>
<body>
<div id="myDIV"></div>
<p id="demo"></p>
<script>
document.getElementById("myDIV").addEventListener("mousemove", function(event) {
  myFunction(event);
});

function myFunction(e) {
  var x = e.clientX;
  var y = e.clientY;
  var coor = "Coordinates: (" + x + "," + y + ")";
  document.getElementById("demo").innerHTML = coor;
}
</script>

</body>
</html>
Bubbles:
Cancelable:
Event type:
Supported HTML tags:











Yes
Yes
MouseEvent
All HTML elements, EXCEPT:
<base>,
<bdo>,
<br>,
<head>,
<html>,
<iframe>,
<meta>,
<param>,
<script>,
<style>, and
<title>



PreviousNext

Related