Javascript DOM MouseEvent clientY Property get

Introduction

Output the coordinates of the mouse pointer while the mouse pointer moves over an element:

Mouse over the rectangle above to get the horizontal and vertical coordinates of your mouse pointer.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//www  .  j a  v a2  s.com
  width: 200px;
  height: 100px;
  border: 1px solid black;
}
</style>
</head>
<body>

<div onmousemove="showCoords(event)" onmouseout="clearCoor()"></div>
<p id="demo"></p>

<script>
function showCoords(event) {
  var x = event.clientX;
  var y = event.clientY;
  var coor = "X coords: " + x + ", Y coords: " + y;
  document.getElementById("demo").innerHTML = coor;
}

function clearCoor() {
  document.getElementById("demo").innerHTML = "";
}
</script>

</body>
</html>



PreviousNext

Related