
- 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
JavaScript – Getting Coordinates of mouse
To get the coordinates of mouse using JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 25px; font-weight: 500; } </style> </head> <body> <h1>Coordinates of mouse</h1> <p class="sample"></p> <h3>Hover the mouse over the screen to get its x and y axis position</h3> <script> let sampleEle = document.querySelector(".sample"); document.body.addEventListener("mousemove", (event) => { sampleEle.innerHTML = "X axis: " + event.x + " Y axis: " + event.y; }); </script> </body> </html>
Output
The above code will produce the following output:
- Related Articles
- Store mouse click event coordinates with Matplotlib
- How to show mouse release event coordinates with Matplotlib?
- Matplotlib – How to show the coordinates of a point upon mouse click?
- How to draw a line following mouse coordinates with tkinter?
- Trigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?
- What is the role of altKey Mouse Event in JavaScript?
- What is the role of clientX Mouse Event in JavaScript?
- What is the role of clientY Mouse Event in JavaScript?
- What is the role of ctrlKey Mouse Event in JavaScript?
- What is the role of pageX Mouse Event in JavaScript?
- What is the role of pageY Mouse Event in JavaScript?
- What is the role of screenX Mouse Event in JavaScript?
- What is the role of screenY Mouse Event in JavaScript?
- What is the role of shiftKey Mouse Event in JavaScript?
- Circle coordinates to array in JavaScript

Advertisements