Friday, September 5, 2025
HomeLanguagesJavascriptJavaScript Coordinates of Mouse

JavaScript Coordinates of Mouse

In JavaScript, we have methods that help us to capture the location of the mouse on the screen. The top left corner of the screen is (0, 0) i,e, X and Y coordinates are (0, 0). This means that vertical zero is the topmost point and horizontal zero is the leftmost point. 
So, the task is to find the coordinates of the mouse over the screen.

It can be implemented using the clientX and clientY methods of the event: 

  • event.clientX: It gives the horizontal coordinate of the event.
  • event.clientY: It gives the vertical coordinate of the mouse.

Syntax: 

// For X coordinate
event.ClientX
// For Y coordinate 
event.ClientY

Example: 

HTML




<script>
    function coordinate(event) {
        var x=event.clientX;
        var y=event.clientY;
        document.getElementById("X").value=x;
        document.getElementById("Y").value=y;
    }
</script>
  
<body onmousemove="coordinate(event)">
    X-coordinate
    <input type="text" id="X">
    <br>
    <br>
    Y-coordinate
    <input type="text" id="Y">
</body>


Output: 

 

 

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS