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!

Szarik
  • 17
  • 2
  • 8

1 Answers1

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

Community
  • 1
  • 1
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
  • 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. – Felix Kling Dec 13 '14 at 21:44