jQuery $.on.apply not working
Asked Answered
M

3

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 :/

Maltha answered 9/12, 2014 at 8:30 Comment(4)
what are you trying to achieve??Stricklin
I'm trying to bind events on a custom objectMaltha
apply of undefined shows $ is undefined.Suffer
Giyf: #3797288Accompaniment
N
3

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

$.fn.on.apply(this, args);
Naman answered 9/12, 2014 at 8:32 Comment(3)
now I get Uncaught TypeError: undefined is not a functionMaltha
what is the jQuery versionNaman
it's the latest. it works if I use it with html elementMaltha
G
1

I think your syntax is incorrect. use syntax like

$('el').on("click", function() {
       alert( $( this ).text() );
       });
Geostrophic answered 9/12, 2014 at 8:33 Comment(1)
instead of $(this).text, you could just write this.textContent, though. It saves you a number of function callsSilvester
S
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

Silvester answered 9/12, 2014 at 8:35 Comment(3)
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: forum.jquery.com/topic/…Maltha
found the info here: #1553842. It seems to work if I call on() normally, but if I use apply I get that weird error :/Maltha
@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, imoSilvester

© 2022 - 2024 — McMap. All rights reserved.