Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - using JS events like "dragover"

I've got a JavaScript function with an eventListener for `dragover.

It looks like this:

    document.getElementById("someID").addEventListener("dragover",
        function(){ 
            //Do logic
        }, 
     false);

The thing is - someID will be a dynamic element - it gets removed and added on the page. After it's removed and added back in, the eventListener will no longer pickup the dragover event. The only way I know of how to deal with this situation is to use jQuery's .on()

My problem: I can't find the dragover event under jQuery API... Does it even exist? If not how can I use it in jQuery?

like image 469
Allen S Avatar asked Oct 17 '25 05:10

Allen S


1 Answers

You can define a customer drag event as,

(function(jQuery) {

//Defining drag event.
jQuery.fn.drag = function() {
    eventType = arguments[0] || 'dragstart';
    onEvent = typeof arguments[1] == 'function' ? arguments[1] : function() {
    };
    eventOption = arguments[2] || false;

    $(document).on('mouseover', $(this).selector, function() {
        if ($(this).hasClass(eventType)) {
            return;
        }
        if (! typeof eventType == 'string') {
            return;
        }

        console.log('Binding Drag Event');
        $(this).each(function() {
            $(this)[0].addEventListener(eventType, onEvent, eventOption);
            $(this).addClass(eventType);
        })
    });
}
})(jQuery);

And then you have to add this as a plugin and apply it for required selector as follows.

$('#someID').drag('dragover', function(){ 
        //Do logic
        alert('DRAGGING!!!!!!');
    }, 
 false);

Working fiddle http://jsfiddle.net/sadepu/aDYfP/

like image 93
Sai Chaithanya Avatar answered Oct 18 '25 21:10

Sai Chaithanya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!