Wednesday, July 1, 2009

[jQuery] Plugins and context

Is there a way for plugins to know the context of the jQuery object?

According to the jQuery Core docs, I can create a jQuery collection by passing in a selector and a context, and I should be able to retrieve the context from the object's context property, but it's not working within plugins.

For example, here's a simple plugin and some code to run it:

$.fn.myPlugin = function()
{
    console.log('selector:', this.selector); // this works
    console.log('context:', this.context); // this doesn't work
};

var foo = $('#foo');
$('p', foo).myPlugin();

Is the context property only available outside the scope of plugins?

--
Hector

No comments: