How to get coordinates of mouse-clicks? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to get coordinates of mouse-clicks?

I have a div element with id = display-image, In the element images are displayed. I want to know the coordinates when the mouse clicks on the image only. Since the image fits in the div element, then the coordinate of the any click within display-image div needs to be recorded.

12th Mar 2022, 7:44 PM
Akash
Akash - avatar
2 Answers
+ 3
The click event will provide an event object, the coordinates of the mouse click are properties of click event object. You can get the coordinates relative to the window, document or target element. The code provided is showing the coordinates relative to the target element, event.offsetX and event.offsetY https://code.sololearn.com/Wf0gjzpbZZCk The MouseEvent Object https://www.w3schools.com/jsref/obj_mouseevent.asp
12th Mar 2022, 10:13 PM
ODLNT
ODLNT - avatar
12th Mar 2022, 9:12 PM
Lisa
Lisa - avatar