Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing functions bound to event handlers with jQuery

With jQuery you can bind functions to an event triggered on a DOM object using .bind() or one of the event handler helper functions.

jQuery have to store this internally somehow and I wonder if is it possible given a DOM object, to find out which events have been bound to the object, and access those functions etc. The desired return result could look something like this:

{   click: [function1, function2],   change: [function3],   blur: [function4, function5, function6] } 
like image 489
googletorp Avatar asked Mar 05 '10 15:03

googletorp


People also ask

Which jQuery method is used to attach a handler to an event?

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.

How is an event handled in jQuery?

Whenever a jQuery event is fired, jQuery passes an Event Object to every event handler function. The event object provides various useful information about the event.

Which jQuery method triggers or binds a function to the error event of selected elements?

jQuery error() Method The error() method triggers the error event, or attaches a function to run when an error event occurs.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

jQuery 1.7 has stopped exposing the events in the regular data() function. You can still get them like this:

var elem = $('#someid')[0]; var data = jQuery.hasData( elem ) && jQuery._data( elem ); console.log(data.events); 

Please note, that this only works for Events which have been bound using jQuery. AFAIK you there is no way to see all the events which have been bound using the regular DOM functions like addEventListener.

You can see them in the webkit inspector though: In the Elements tab navigate to the desired DOM node, on the right side select the "Event Listeners" drop down.

like image 133
eikes Avatar answered Sep 27 '22 01:09

eikes