How do I get the x and y of a click using javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I get the x and y of a click using javascript?

So Im making a 2d html game and Im tryna get the x and y of a click and turn it into a variable. How would I do that? Edit:I've been trying a different method but it broke the game for some reason so I was stuck. Thnx

4th Dec 2019, 6:01 AM
Lemonades For Sale
Lemonades For Sale - avatar
1 Answer
+ 3
i use the event object parameter in the callback function for that. for example, in the addeventlistener: addEventListener("click", function(event) { console.log(event.x); console.log(event.y); }); The above function will be fired when the user clicks anywhere in the document. https://code.sololearn.com/WKOTJchCY1IK/?ref=app
4th Dec 2019, 6:07 AM
Nootnoot
Nootnoot - avatar