0

This is what I have that I'm trying to access:

$("some_element").bind("change", some_func);

What I'm doing now returns an object, but does not allow you to call it like you normally can when assigning an anonymous function:

//Anonymous function storing
var func = function() {
    //Blah
};
//What I'm currently doing to try and access the function bound to some_element
var func = $("some_element").bind("change");

Which just returns:

alert(func); //[object Object]

I'd assume that's because it returns as an object, but not necessarily a function? Or maybe jQuery doesn't allow doing this?

UPDATE: Edited original question for clarity. The suggestion I found floating around the web is to use the $._data method for retrieving all information about events; HOWEVER, the articles (on StackOverflow, mostly) I read touting what method the modern version of jQuery supports do not work.

What I need to do is somehow get some_func and be able to call it elsewhere in my script:

//First accessing
var some_func = $("some_element").bind("click");
//Then calling elsewhere
some_func.apply();
  • 4
    Uhm, what ? ............ – adeneo Jan 05 '15 at 22:58
  • 1
    What are you trying to do here? – Mr. Concolato Jan 05 '15 at 23:00
  • If you want to find the event bound with `.bind("change",callback)` see the linked duplicate. The reason you returned `[object Object]` is that `.bind()` returns `this` which is the jQuery object matching the original selector `("some_element")`. Sending it to alert calls toString on it hence the string "[object Object]". – Travis J Jan 05 '15 at 23:04
  • Okay, so I checked out the solution here: [Solution](http://stackoverflow.com/questions/2008592/can-i-find-events-bound-on-an-element-with-jquery/27789580#27789580), which seems to work. So, if I were to do something like `var events = $._data($($select)[0],"events"); for (var e in events) { alert((typeof events[e][0]).toString()); }` to traverse events, interestingly enough, both `events[e] and events[e][0]` also return objects. The duplicate post does not show how to actually access function so that you may call it. –  Jan 05 '15 at 23:22
  • Nevermind. Did some more searching and found [this article](http://stackoverflow.com/questions/2518421/jquery-find-events-handlers-registered-with-an-object), which can be used for showing function contents as well as calling. –  Jan 05 '15 at 23:45

0 Answers0