Search code examples
javascriptjquerydraggabledrag

How do I stop mousemove?


I have created a JS function to drag an item left & right. I need to stop the dragging after clicking a button. How do I stop mousemove?

I've tried $('.draggable').off('mousemove'); and e.stopPropagation();, but neither worked for me.

A JSFiddle is worth a thousand words, please see the issue here: https://jsfiddle.net/kthornbloom/k2k928j1/


Solution

  • Your problem is that you're doing

    $('.draggable').parents().on('mousemove', function(e) { ... });
    

    and

    $('.draggable').off('mousemove');
    

    The off call isn't on the same element as the on call. Change it to

    $('.draggable').parents().off('mousemove');
    

    and it works.

    ...although you really ought to be storing the handler and removing it explicitly.