aambrozkiewicz
aambrozkiewicz

Reputation: 552

Own jQuery plugin, scope problem

I want to make a plugin, which is ment to work with plain objects - I can iterate using each on. Thus i want to be accessible through jQuery's global object like $.myPluginFn().

(function($) { $.fn.test = function() { return 1; } })(jQuery)

Executing $j.test() gives an error:

TypeError: Object function (a,b){return new d.fn.init(a,b,g)} has no method 'test'

What am I doing wrong? Assigning to $.fn.myPluginFn works on the other hand.

Upvotes: 0

Views: 303

Answers (2)

Alnitak
Alnitak

Reputation: 340055

If I understand you correctly and you want jQuery.test() to work you should be using:

$.test = ...

instead of

$.fn.test = ...

The latter is what you would use if you wanted jQuery(selector).test() to work.

Upvotes: 2

the .fn.foo function gives a foo function to each jQuery Object. So if you create one you can call it like this: $("#id_of_item").foo();

you this is useful if yo want to manipulate items.

but if you want to create other functions you dont need to put into the jquery object.

Upvotes: 1

Related Questions