Preventing click event with jQuery drag and drop

jquery draggable click event
vue click not drag
jquery draggable disable
drag and drop copy instead of move in jquery
on click but not on drag
jquery preventdefault
jquery distinguish click from drag
jquery drag and drop list

I have elements on the page which are draggable with jQuery. Do these elements have click event wich navigates to another page (ordinary links for example).

What is the best way to prevent click from firing on dropping such element while allowing clicking it is not dragged and drop state?

I have this problem with sortable elements but think it is good to have a solution for general drag and drop.

I've solved the problem for myself. After that I found that same solution exists for Scriptaculous, but maybe someone has a better way to achieve that.


A solution that worked well for me and that doesn't require a timeout: (yes I'm a bit pedantic ;-)

I add a marker class to the element when dragging starts, e.g. 'noclick'. When the element is dropped, the click event is triggered -- more precisely if dragging ends, actually it doesn't have to be dropped onto a valid target. In the click handler, I remove the marker class if present, otherwise the click is handled normally.

$('your selector').draggable({
    start: function(event, ui) {
        $(this).addClass('noclick');
    }
});

$('your selector').click(function(event) {
    if ($(this).hasClass('noclick')) {
        $(this).removeClass('noclick');
    }
    else {
        // actual click event code
    }
});

prevent click event after drag in jQuery, jQuery and jQuery UI has cool stuff that makes drag and drop easy with jQuery draggable and jQuery droppable if you want the droppable  jQuery and j Query UI has cool stuff that makes drag and drop easy with jQuery draggable and jQuery droppable if you want the droppable functionality. With draggable however, if the element or element’s child is capturing click events or an anchor tag with a link, it triggers the click event and follows the link after you are done dragging.


Solution is to add click handler that will prevent click to propagate on start of drag. And then remove that handler after drop is performed. The last action should be delayed a bit for click prevention to work.

Solution for sortable:

...
.sortable({
...
        start: function(event, ui) {
            ui.item.bind("click.prevent",
                function(event) { event.preventDefault(); });
        },
        stop: function(event, ui) {
            setTimeout(function(){ui.item.unbind("click.prevent");}, 300);
        }
...
})

Solution for draggable:

...
.draggable({
...
        start: function(event, ui) {
            ui.helper.bind("click.prevent",
                function(event) { event.preventDefault(); });
        },
        stop: function(event, ui) {
            setTimeout(function(){ui.helper.unbind("click.prevent");}, 300);
        }
...
})

jQuery Draggable – Prevent Click Event, I was wondering if anyone had an issue with click event being fired after drag and drop. My container is a huge list of images wrapped with links  My problem is that if you drag the map, on mouse up, the click event is fired on whatever child div you started the drag from. How do I stop the mouse up from triggering the click event if its part of a drag (as opposed to someone just clicking without a drag, in which case the click event is desired).


I had the same problem and tried multiple approaches and none worked for me.

Solution 1

$('.item').click(function(e)
{            
    if ( $(this).is('.ui-draggable-dragging') ) return false;
});  

does nothing for me. The item is being clicked after the dragging is done.

Solution 2 (by Tom de Boer)

$('.item').draggable(
{   
    stop: function(event, ui) 
    {
         $( event.originalEvent.target).one('click', function(e){ e.stopImmediatePropagation(); } );
    }
});

This works just fine but fails in one case- when I was going fullscreen onclick:

var body = $('body')[0];     
req = body.requestFullScreen || body.webkitRequestFullScreen || body.mozRequestFullScreen;
req.call(body); 

Solution 3 (by Sasha Yanovets)

 $('.item').draggable({
        start: function(event, ui) {
            ui.helper.bind("click.prevent",
                function(event) { event.preventDefault(); });
        },
        stop: function(event, ui) {
            setTimeout(function(){ui.helper.unbind("click.prevent");}, 300);
        }
})

This does not work for me.

Solution 4- the only one that worked just fine

$('.item').draggable(
{   
});
$('.item').click(function(e)
{  
});

Yep, that's it- the correct order does the trick- first you need to bind draggable() then click() event. Even when I put fullscreen toggling code in click() event it still didn't go to fullscreen when dragging. Perfect for me!

Prevent click propagation on drag and drop · Issue #19 · asvd , In order to make this happen i must find a way so as the click event wont happen on drag and drop. I could'n find any answer to that. So i guess  I have some elements on a page which are draggable. These same elements have a click event which navigates to another page. I'm trying to determine the best way of preventing the click event from firing if the user is dragging but still allow the click event if not dragging. Anyone have any ideas of the best way to accomplish this?


I'd like to add to this that it seems preventing the click event only works if the click event is defined AFTER the draggable or sortable event. If the click is added first, it gets activated on drag.

Can the same element have .draggable() and click event?, How can I prevent the native click event from triggering? I tried all jQuery methos "stoppropagation", "stopImmediatePropagation" and "  The ondrag event occurs when an element or text selection is being dragged. Drag and drop is a very common feature in HTML5. It is when you "grab" an object and drag it to a different location. For more information, see our HTML Tutorial on HTML5 Drag and Drop. Note: To make an element draggable, use the global HTML5 draggable attribute.


I don't really like to use timers or preventing, so what I did is this:

var el, dragged

el = $( '#some_element' );

el.on( 'mousedown', onMouseDown );
el.on( 'mouseup', onMouseUp );
el.draggable( { start: onStartDrag } );

onMouseDown = function( ) {
  dragged = false;
}

onMouseUp = function( ) {
  if( !dragged ) {
    console.log('no drag, normal click')
  }
}

onStartDrag = function( ) {
  dragged = true;
}

Rocksolid..

draggable - 'click' triggered on drag - GSAP, In the modern HTML standard there's a section about Drag and Drop with For instance, we can't prevent dragging from a certain area. ball . onmousedown = function ( event ) { // (1) prepare to moving: The examples fail if the cursor is dragged outside of the document window and the click is released. The 'dragover' and 'dragleave' events work fine, displaying an inset border around the entire page when I drag a file over an removing it if I drag the file out again. However, the 'drop' event doesn't seem to fire at all, the dropped file simply opens in the browser window.


Drag'n'Drop with mouse events, If you want not just drag, but drag & drop, see the jQuery UI Droppable plugin, which provides a drop target This option can be used to prevent unwanted drags when clicking on an element. Bind an event listener to the dragcreate event:  Prevent Image Dragging using JavaScript “ondragstart” event. The JavaScript ondragstart event occurs when a user drags an HTML element (in our case it’s an Image) on a web page. An image on a web page is draggable by default. We can use the ondragstart event to prevent dragging of the image. Syntax. object.ondragstart = function { your


Draggable Widget, There are many events that are used, and can occur, in the different stages of a drag and drop operation: Events fired on the draggable target (the source element):. dans jQuery UI, les éléments traînés sont classés dans la classe"ui-draggable-dragging". Nous pouvons donc utiliser cette classe pour déterminer s'il faut cliquer ou non, il suffit de retarder l'événement.


ondrag Event, To add drag-and-drop functionality to your pages, you need to include both the jQuery library and the jQuery UI plugin. jQuery UI is a fantastic plugin for jQuery that adds all sorts of useful user interface widgets, effects and behaviours — including drag-and-drop.