Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I learn if events are bound to an element using jquery?

Tags:

jquery

events

I need a hasEvents() method like

var someBool = hasEvents($("#myelement"));

that returns true if there are some bound events to any of the element's event handlers.

like image 304
Serhat Ozgel Avatar asked Nov 19 '08 22:11

Serhat Ozgel


People also ask

How do you find the events of an element?

right click on the target element -> select " Inspect element " Scroll down on the right side of the dev frame, at the bottom is ' event listeners '. Expand the tree to see what events are attached to the element.

Which jQuery keyword should be used to bind an event to an element?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.

What is the difference between bind () and live () method in jQuery?

In short: . bind() will only apply to the items you currently have selected in your jQuery object. . live() will apply to all current matching elements, as well as any you might add in the future.

What is event binding in jQuery?

jQuery provides a method . on() to respond to any event on the selected elements. This is called an event binding.


1 Answers

have you taken a look at the hasEvent() plugin? the code is pretty small:

(function(A) {
    A.fn.hasEvent = function(C) {
       var B = this.data("events");
       return( B && B[C] )
    }
}) (jQuery)

for your specific purpose you could modify it slightly:

(function($) {
    $.fn.hasEvents = function() {
        return new Boolean(this.data('events') );
    }
}) (jQuery);

$('#someDiv').click(function() {
    alert('new event');
});

$('#someDiv').hasEvents();      // true
$('#someOtherDiv').hasEvents(); // false
like image 149
Owen Avatar answered Oct 17 '22 05:10

Owen