Get X / Y coordinates on a button using "onclick" and JavaScript

I am starting JavaScript and looking for a way to get the x and y coordinates on a button when clicked. This works in Opera, IE9 and Chrome, but I can't get it to work in Firefox. Here is my code:

Function in JavaScript:

function buttonClick(subEvent) { var mainEvent = subEvent ? subEvent : window.event; alert("This button click occurred at: X(" + mainEvent.screenX + ") and Y(" + mainEvent.screenY + ")"); } 

Here is the HTML section:

 <input type="button" onclick="buttonClick()" value="Submit"/> 

The idea here is to get the coordinates only when the button is pressed and get the actual coordinates within the button itself. Getting the coordinates on the screen is easier and finding that the solution for all browsers has already been completed.

Thanks in advance for any help.

+7
source share
1 answer

You need to pass the event. This is done like this:

 <input type="button" onclick="buttonClick(event)" value="Submit"/> ^ '---- that one there 
+8
source

All Articles