Javascript examples for DOM:Mouse Event
The pageX property returns the horizontal coordinate of the mouse pointer.
This property is read-only.
A Number, horizontal coordinate of the mouse pointer, in pixels
The following code shows how to Output the coordinates of the mouse pointer when the mouse button is clicked on an element:
<!DOCTYPE html> <html> <head> <style> div {/*from ww w.j av a 2s.c o m*/ 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.pageX; var y = event.pageY; var coor = "X coords: " + x + ", Y coords: " + y; document.getElementById("demo").innerHTML = coor; } function clearCoor() { document.getElementById("demo").innerHTML = ""; } </script> </body> </html>