Javascript DOM MouseEvent screenX Property

Introduction

Get coordinates of the mouse pointer, relative to the screen, 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, relative to the screen,//from w ww. j a  v  a2  s  .com
when it is clicked.</h2>
<p id="demo"></p>

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

</body>
</html>

The screenX property returns the horizontal coordinate to the users computer screen of the mouse pointer when an event was triggered.

This property is read-only.

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




PreviousNext

Related