
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to find the coordinates of the cursor with JavaScript?
In this tutorial, we will learn how we can find the coordinates of the mouse cursor with JavaScript. We have to find out the X and Y-coordinates or we can say that the horizontal and the vertical position of the cursor on the screen with JavaScript.
JavaScript provides us with two different properties to get the coordinates of the mouse cursor when the mouse button is pressed anywhere on the screen −
Using event.clientX Property
Using event.clientY Property
Using event.clientX Property
The event.clientX property is used to find out the value of the horizontal position or the X-coordinate of the cursor where the event was triggered. It returns a numeric value that specifies the horizontal position of the cursor.
Syntax
Following is the syntax to get the horizontal position of the cursor −
function function_name(event){ let X=event.clientX; }
Steps
Step 1 − In step one, we need to define the callback function that performs some activity when the event triggers.
Step 2 − In the second step, we will declare a variable and assign it a value using event.clientX property, that returns the horizontal or the X-coordinate of the cursor position.
Step 3 − This step contains the statements that are required to display the returned value on the screen that is stored in the variable declared in last step.
Example
Below example illustrates how we can get the X-coordinate of the cursor using JavaScript −
<!DOCTYPE html> <html onclick="display(event)"> <body> <h3>Find the coordinates of the cursor with JavaScript</h3> <p id="para">Click anywhere on the screen to see X-coordinate of the cursor using "event.clientX" property.</p> <p id="result"></p> <script> function display(event) { let X = event.clientX; let result = document.getElementById("result"); result.innerHTML = "<b>X-coordinate: </b>" + X; } </script> </body> </html>
In this example, we find out the position of the cursor on X-axis or in horizontal direction using the event.clientX property.
Using event.clientY Property
It is used to find out the position of the cursor in the vertical direction or on the Y-axis where the event was triggered. Similar to the event.clientX property, it also returns a numeric value that holds the position of the cursor on the Y-axis or Y-coordinate.
Syntax
Following is the syntax to use event.clientY property to get the Y-coordinate of cursor using JavaScript −
function function_name(event){ let Y=event.clientY; }
Steps
Step 1 − In step one, we will define the callback function that performs some activity on the webpage when the event triggers.
Step 2 − In second step, we will declare a variable and assign it a value using the event.clientY property, that returns the vertical or the Y-coordinate of the cursor position.
Step 3 − This step includes the statements that are required to display the returned value of the cursor position on the Y-coordinate or in the vertical direction on the screen that is stored in the variable declared in the last step.
Example
Below example illustrates how we can get the X-coordinate of the cursor using JavaScript −
<!DOCTYPE html> <html onclick="display(event)"> <body> <h3>Find the coordinates of the cursor with JavaScript</h3> <p id="para">Click anywhere on the screen to see Y-coordinate of the cursor using "event.clientY" property.</p> <p id="result"></p> <script> function display(event) { let Y = event.clientY; let result = document.getElementById("result"); result.innerHTML = "<b>Y-coordinate: </b>" + Y; } </script> </body> </html>
In this example, we have find out the Y-coordinate of the cursor using The event.clientY property of JavaScript.
Let us understand how we can find out the position of the cursor on a 2D plane using both properties in the same example.
Steps
Step 1 − In step one, we will define the callback function that performs some activity on the webpage when the event triggers.
Step 2 − In second step, we will declare two variables and assign them values using the event.clientX and the event.clientY properties, which return the horizontal and the vertical or the X and the Y-coordinates of the cursor position on the screen.
Step 3 − This step includes the statements that are required to display the returned value of the cursor position on the Y-coordinate or in the vertical direction on the screen that is stored in the variable declared in the last step.
Example
Below example illustrates how we can get the X-coordinate of the cursor using JavaScript −
<!DOCTYPE html> <html onclick="display(event)"> <body> <h3>Find the coordinates of the cursor with JavaScript</h3> <p id="para">Click anywhere on the screen to see X and Y-coordinate of the cursor using JavaScript properties.</p> <p id="result"></p> <script> function display(event) { let X = event.clientX; let Y = event.clientY; let result = document.getElementById("result"); result.innerHTML = "<b>X-coordinate: </b>" + X + "<br><b>Y-coordinate: </b>: " + Y; } </script> </body> </html>
In above example, we have used the event.clientX and event.clientY properties simultaneously to find out the position of cursor on 2D plane using JavaScript.
In this tutorial, we have learnt about the JavaScript properties to find out the coordinates of the cursor with help of individual and a example where we used both the properties to find the position of cursor on 2D plane or X and Y-coordinates simultaneously.
- Related Articles
- How to find the coordinates of the cursor relative to the screen with JavaScript?
- How to set the type of cursor to display for the mouse pointer with JavaScript?
- How to set the cursor to wait in JavaScript?
- JavaScript code to find the coordinates of every link in a page
- How to find the number of anchors with JavaScript?
- How to find the value of a button with JavaScript?
- FabricJS – How to find the real center coordinates of an Image object?
- How to find the real center coordinates of a Line object using FabricJS?
- JavaScript Cursor property
- How to change cursor to waiting state in JavaScript/jQuery?
- Specify the color of the cursor in elements with CSS
- How to find the current cursor position on the clicked Polyline object using FabricJS?
- How to change the visibility of the Cursor of Console in C#
- How to find the text visible on the button with JavaScript?
- JavaScript – Getting Coordinates of mouse
