question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't prevent click event after div dragEnd

See original GitHub issue

I use Interact with angular and have ng-click handler on element. Click has called after my dragend event. How I can prevent that? Where I can add

.on('dragend', function (event) { 
  event.interactable.preventDefault(true); // doesn't work
  event.interactable.options.preventDefault = true ; // doesn't work
  event.preventDefault() ; // doesn't work

or smth else?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tayecommented, Jun 22, 2015

I would think that this is really more of an Angular issue since interact.js’ tap event exists to bypass click problems.

There’s no default action from InteractEvents so their preventDefault method does nothing. Also, calling preventDefault on mousedown or mouseup events doesn’t stop clicks from happening. If you want to stop the click event then you should add a click event listener with useCapture that just calls event.stopImmediatePropagation().

interact(target).on('click', function (event) {
  event.stopImmediatePropagation();
}, true /* useCapture */);
2reactions
SlyDavecommented, Jun 21, 2021

I know this is an old issue, I just wanted to address the adding of the event every *end (resizeend, dragend etc) being a bad idea, simple add the once option to the Listener.

.on('dragend', function (event) {
    event.target.addEventListener('click', (event) => event.stopImmediatePropagation(), {capture: true, once: true});
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent click event if dragged - javascript - Stack Overflow
I think how it works is that click fires after a mouseup and mousedown ... is by disabling pointer events for that element...
Read more >
Drag'n'Drop with mouse events - The Modern JavaScript Tutorial
Use event delegation to track drag start: a single event handler on document for mousedown . If elements are dragged to top/bottom window...
Read more >
An Essential Guide to JavaScript Drag and Drop By Examples
This tutorial introduces you to the JavaScript Drag and Drop API and shows you how to handle drag and drop events effectively.
Read more >
ondrop Event - W3Schools
The ondrop event occurs when a draggable element or text selection is dropped on a valid drop target. Drag and drop is a...
Read more >
Using A "Drag Shield" To Block Mouse Events ... - Ben Nadel
To prevent these unwanted interactions, I ended up using what I'm calling a "drag shield". This was a fixed-position DIV element that I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found