Javascript DOM MouseEvent pageX 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 and y coordinates of the 
mouse pointer when it was clicked.</h2>
<p id="demo"></p>
<script>
function showCoords(event) {/* www . jav a2 s .  co m*/
  var x = event.pageX;
  var y = event.pageY;
  var coords = "X coords: " + x + ", Y coords: " + y;
  document.getElementById("demo").innerHTML = coords;
}
</script>

</body>
</html>

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

This property is read-only.




PreviousNext

Related