10

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

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 only get the coordinates when the button is clicked and to get the actual coordinates within the border of the button itself. Getting coordinates on the screen is easier and finding that solution for all browsers was already accomplished.

Thanks in advance for any assistance.

2
  • um you don't get an X and Y as a child element of TD the X and Y positions are governed by the table layout Commented Apr 26, 2011 at 23:49
  • I think that just placed the button itself inside a table. To be clearer, I removed that code section... Thanks. Commented Apr 26, 2011 at 23:54

1 Answer 1

13

You need to pass on the event. This is done as so:

<input type="button" onclick="buttonClick(event)" value="Submit"/>
                                            ^
                                            '---- that one there
Sign up to request clarification or add additional context in comments.

1 Comment

Damn! See, this is a great example of a plain 'ol misunderstanding. I actually read this somewhere else, but the example made it seem like the 'event" was the variable, so I placed mine in place of 'event' like so onclick="buttonClick(subEvent)" and it didn't work. But you actually need the word 'event' in there... Geez... Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.