jQuery trigger function when mousedown or touchend event happen outside selector area

1.4k views Asked by At

Scenario: user clicks on a element, then drags it. Dragging ends elsewhere though. I noticed that when this happens, if I listen to the element that's being dragged, it won't trigger anything unless the cursor is released on the same element when dragging finishes.

How do I trigger an event that starts on an element but ends anywhere else on document?

$('#hotels-slider-handle').on('mouseup touchend click', function() {
    $('#booking-input').trigger('change');
});
1

There are 1 answers

0
unfulvio On

in answer to Cerlin Boss who commented to my original post above... this is what I came up with and works well (what I need to trigger gets triggered just fine) - I was looking for more efficient solutions though (ignore the jQuery delays, they're necessary in my scope)

var bookingFormInputs = $('#booking-input'),
    hotelsSlider = $('#hotels-slider');
$('#hotels-slider-handle').on('mousedown touchstart', function() {
    $(document).delay(300).one('mouseup touchend', function() {
        $(bookingFormInputs).delay(600).trigger('change');
    });
    $(hotelsSlider).delay(300).one('mouseleave', function() {
        $(document).on('mouseup touchend', function() {
            $(bookingFormInputs).delay(300).trigger('change');
        });
    });
});