1

Any one know why

$.on.apply(this, args);

gives me "Uncaught TypeError: Cannot read property 'apply' of undefined"

I know for sure that $.on is defined in jQuery :/

Elfy
  • 1,733
  • 6
  • 19
  • 39

3 Answers3

3

The on() method is bound to $.fn $

$.fn.on.apply(this, args);
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

I think your syntax is incorrect. use syntax like

$('el').on("click", function() {
       alert( $( this ).text() );
       });
fly_ua
  • 1,034
  • 8
  • 12
1

You know for sure $.on is defined, well... I beg to differ:

console.log($.on);//undefined
console.log($.fn.on);//function (a,c,d,e,f)
console.log($().on);//function...

Not sure though, that what you're trying to do is a valid use-case for jQuery's on, besides, applying the on function to a custom object doesn't give that object magical properties... I mean: it's not like that object will all of a sudden become clickable, if it's just an object literal

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • I know, I'm just trying to make use of jQuery's event system in my own app. ppl say that you can use events on custom objects: https://forum.jquery.com/topic/triggering-custom-events-on-js-objects#14737000000743154 – Elfy Dec 09 '14 at 08:39
  • found the info here: http://stackoverflow.com/questions/1553342/custom-event-in-jquery-that-isnt-bound-to-a-dom-element. It seems to work if I call on() normally, but if I use apply I get that weird error :/ – Elfy Dec 09 '14 at 08:40
  • @Elfy: jQuery doesn't actually have its own event system, it merely hatches on to JS's event loop, and provides a consistent API for it. You can register _"fake"_ events, though, and manually invoke the handler: `jQObject.trigger('myevent');`, but that still means you have to _manually_ trigger the event, and rather defeats the point, imo – Elias Van Ootegem Dec 09 '14 at 09:27