MouseEvent screenX Property - Javascript DOM

Javascript examples for DOM:Mouse Event

Description

The screenX property returns the horizontal coordinate of the mouse pointer.

This property is read-only.

Return Value

A Number, representing the horizontal coordinate of the mouse pointer, in pixels

The following code shows how to get coordinates of the mouse pointer, relative to the screen.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<h2 onclick="showCoords(event)">Click this heading.</h2>

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

<script>
function showCoords(event) {// ww  w .  j a va2 s  .  c  o  m
    var x = event.screenX;
    var y = event.screenY;
    var coords = "X coords: " + x + ", Y coords: " + y
    document.getElementById("demo").innerHTML = coords;
}
</script>

</body>
</html>

Related Tutorials