

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Matplotlib – How to show the coordinates of a point upon mouse click?
- Store mouse click event coordinates with Matplotlib
- How to show mouse release event coordinates with Matplotlib?
- How to draw a line following mouse coordinates with tkinter?
- Rust programming language – Getting Started
- Express.js – Getting Remote Client Address
- Circle coordinates to array in JavaScript
- Trigger an event IMMEDIATELY on mouse click, not after I let go of the mouse - JavaScript?
- How to find the coordinates of the cursor with 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?
Advertisements