0

I'm trying to create a drag function using a combination of jQuery mousedown/move/up events, during which I'm trying to pass the starting (cursor) position of the mousedown event to the mousemove event handler but am getting the error

Argument of type mousemove is not assignable to the type 'PlainObject...'

Here's my code...

module controls.draganddrop {

    export function init() {
        $("#mainContent").on("mousedown", ".itemList > ul > li > p", mouseDown);
        $("#mainContent").on("mouseup", ".itemList > ul > li > p", mouseUp);
    }

    function mouseDown(e) {
        var startPosition: object = { x: e.pageX, y: e.pageY };
        $(".itemList > ul > li > p").on("mousemove", mouseMove(event, startPosition));
    }

    function mouseMove(e, startPosition) {
        console.log("pageX: " + e.pageX + ", pageY: " + e.pageY);
    }

    function mouseUp(e) {
        $(".itemList > ul > li > p").off("mousemove", mouseMove);
    }
}

The error is on "mousemove" within the mouseDown function. Clearly I've misunderstood something in the jQuery documentation and/or solutions in similar problems, what am I doing wrong?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
James Morrison
  • 1,954
  • 2
  • 21
  • 48
  • 1
    The duplicate I marked references a click event, but the issue is the same. You're assigning the *returned value* from `mouseMove(event, startPosition)` (ie. `undefined`) to the event handler, not that function itself. You need to wrap that function call in another anonymous function. – Rory McCrossan Apr 05 '18 at 13:53
  • That's certainly resolved the issue, the only problem now is I can't remove the mousemove event handler as it's an anonymous function, is there another way of achieving this? – James Morrison Apr 05 '18 at 13:59
  • 2
    I think `$(".itemList > ul > li > p").off("mousemove");` will remove all handlers from `mousemove` – Scaramouche Apr 05 '18 at 14:01

1 Answers1

0

Try this

$(".itemList > ul > li > p").on("mousemove", function(event){ mouseMove(event, startPosition)});
Matt
  • 2,096
  • 14
  • 20