Javascript DOM MouseEvent clientX Property

Introduction

Output the coordinates of the mouse pointer when the mouse button is clicked on an element:

View in separate window

<!DOCTYPE html>
<html>
<body>

<h2 onclick="showCoords(event)">
  Click this heading to get the x (horizontal) and 
  y (vertical) coordinates of the mouse pointer when it was clicked.</h2>

<p id="demo"></p>

<script>
function showCoords(event) {/*from ww  w  .ja  v  a  2s . c o m*/
  var x = event.clientX;
  var y = event.clientY;
  var coords = "X coords: " + x + ", Y coords: " + y;
  document.getElementById("demo").innerHTML = coords;
}
</script>

</body>
</html>

The clientX property returns the horizontal coordinate to the client area of the mouse pointer when a mouse event was triggered.

The client area is the current window.

This property is read-only.

The clientX property returns a Number representing the horizontal coordinate of the mouse pointer in pixels.




PreviousNext

Related