Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I find events bound on an element with jQuery?

I bind two event handlers on this link:

<a href='#' id='elm'>Show Alert</a> 

JavaScript:

$(function() {   $('#elm').click(_f);   $('#elm').mouseover(_m); });  function _f(){alert('clicked');} function _m(){alert('mouse over');} 

Is there any way to get a list of all events bound on an element, in this case on element with id="elm"?

like image 678
Praveen Prasad Avatar asked Jan 05 '10 19:01

Praveen Prasad


People also ask

How do you find the events of an element?

All you have to do is press F12 to open the developer tools, select the element in the HTML DOM that you want to investigate, on the right side of the window you will see an option called Event Listeners.

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 event binding in jQuery?

The jQuery bind() event is used to attach one or more event handlers for selected elements from a set of elements. It specifies a function to run when the event occurs. It is generally used together with other events of jQuery. Syntax: $(selector).

How do you check if an element is a jQuery element?

In jQuery, you can use the . length property to check if an element exists. if the element exists, the length property will return the total number of the matched elements.


2 Answers

In modern versions of jQuery, you would use the $._data method to find any events attached by jQuery to the element in question. Note, this is an internal-use only method:

// Bind up a couple of event handlers $("#foo").on({     click: function(){ alert("Hello") },     mouseout: function(){ alert("World") } });  // Lookup events for this particular Element $._data( $("#foo")[0], "events" ); 

The result from $._data will be an object that contains both of the events we set (pictured below with the mouseout property expanded):

Console output for $._

Then in Chrome, you may right click the handler function and click "view function definition" to show you the exact spot where it is defined in your code.

like image 173
Sampson Avatar answered Oct 02 '22 09:10

Sampson


General case:

  • Hit F12 to open Dev Tools
  • Click the Sources tab
  • On right-hand side, scroll down to Event Listener Breakpoints, and expand tree
  • Click on the events you want to listen for.
  • Interact with the target element, if they fire you will get a break point in the debugger

Similarly, you can:

  • 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. Not sure if this works for events that are handled through bubbling (I'm guessing not)
like image 24
Vali Shah Avatar answered Oct 02 '22 10:10

Vali Shah