Preventing click event with jQuery drag and drop

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
    }
});

Leave a Comment