0

So I am trying to make script when u press for example "1" then click mouse to send position of cursor, but i have problems with global variables and functions namely with mX and mY

$('#A').on("mousemove", function (e) {  mX = e.pageX });
$('#A').on("mousemove", function (e) {  mY = e.pageY });
...
else if (code == 49) {f(vX, vY, mX, mY);}
...
function f(vX, vY, mX, mY) {
$('#A').click(function (e) {
    var dX = Math.abs(vX - mX); <<< Here is the problem the mX is not defined
    var dY = Math.abs(vY - mY);
});

The problem line i singed as <<< i can't come with up how to solve it, i have make the variables in function to global.

Do not pay attention about code, vX, vY theese variables are working fine!

4
  • 1
    You realize you can get pageX and pageY in the click handler ? Commented Dec 13, 2014 at 21:25
  • Oh man, thats a lots of line of code that working together, i will do this tomorrow, now i am not able to do this, Sorry Commented Dec 13, 2014 at 21:25
  • jsfiddle.net/1fgwhqj9 Commented Dec 13, 2014 at 21:26
  • Oh, its looks like i am very silly, i will try it tomorrow, Thanks man Commented Dec 13, 2014 at 21:35

1 Answer 1

0

Please, refer to How to pass argument to a jquery function through an onClick event?

function f(vX, vY, mX, mY) {
    $('#A').click({mX:mX, mY:mY}, function (e) {
        var dX = Math.abs(vX - e.data.mX);
        var dY = Math.abs(vY - e.data.mY);
    });

hope that's good for you

bye

Sign up to request clarification or add additional context in comments.

1 Comment

I guess that's not good enough since the event handler only has access to the values that mX and mY had at the moment the handler was bound.

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.