Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all live events in jQuery

How could I find in jQuery what events are bound with live for a particular element?

Say I have a function, randomFunction, that returns a random function from an array of functions. How can I find which function has been bound to a certain element?

var arrayOfFunctions = []; //a whole bunch of functions
function randomFunction(array){}; //returns one of those functions
$('#certain_element').live('click', randomFunction(arrayOfFunctions));

What is the index of the array that corresponds to the function that was bound by live for $('#certain_element')?

like image 707
Mario Avatar asked May 26 '11 18:05

Mario


1 Answers

Alright, figured it out. For a click event, for $('#certain_element'), logging each binding's index to the console:

var relevantHandlers = $.map($(document).data('events').live, function(value){
  if(value.origType == 'click' && value.selector == '#certain_element'){
    return value.handler;
  }
}; //all handlers for #certain_element bound to click by live.
$.each(relevantHandlers, function(){
  console.log("the index is: " + $.inArray(this, arrayOfFunctions));
});
like image 76
Mario Avatar answered Oct 19 '22 23:10

Mario