Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | Coordinates of mouse

  • Difficulty Level : Hard
  • Last Updated : 09 Aug, 2021

The top left corner of the screen is (0, 0) i,e, X and Y coordinate is (0, 0). This means that vertical zero is 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 find using the clientX and clientY property: 

  • ClientX: It gives the horizontal coordinate of the event.
  • ClientY: It gives the vertical coordinate of the mouse.


Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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

Below is the required implementation: 


     // coordinate function that calculate the X
     // coordinate and Y coordinates
     function coordinate(event) {
     // clientX gives horizontal coordinate
     var x = event.clientX;
     // clientY gives vertical coordinates
     var y = event.clientY;
     document.getElementById("X").value = x;
     document.getElementById("Y").value = y;
<!-- onmousemove event is called when the mouse
     pointer is moving over the screen -->
<!-- calling of coordinate function -->
<body onmousemove="coordinate(event)">
   <!-- X coordinate is stored in X-coordinate variable -->
   <input type="text" id="X">
   <!-- Y coordinate is stored in Y-coordinate variable -->
   <input type="text" id="Y">



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!