0

How can I call a function of jQuery plugin from inside of the same object. I use exact suggested solution http://docs.jquery.com/Plugins/Authoring#Plugin_Methods. From external code I can call this way:

$('div').tooltip("myMethod", an_attr);

But how can I call the same from inside especially form event when 'this' is not the object of plugin.

var methods = {
    var $this = $(this);
    init : function( options ) {
        $this.click(function(){
            $this.fn2("myMethod", an_attr); //is it right way?

        });
    },
    fn2 : function() {
        //but how can I call the myMethod. here ?
    },
    myMethod : function() {...
2
  • You don't need to wrap this in $(), it's already a jQuery object. Commented Oct 16, 2012 at 15:54
  • Could you elaborate what this should be and what you want to call, in what order? Commented Oct 16, 2012 at 16:03

1 Answer 1

1

In fn2 to invoke myMethod you could do the following:

...
fn2: function() {
  methods.myMethod();
}
...

To be sure that myMethod has the same context as all of the others, you could do:

...
fn2: function() {
  methods.myMethod.call(this);
}
...

More details on call() here.

A JS Fiddle here.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, it works fine. Is there any way to reach DOM node without sending this by the 'call'.
I suppose one answer might be to change each of your functions in methods to always require the jQuery object are the first parameter, and change the line methods[method].apply(this, ...) to ensure the first argument is also this (the jQuery object), then you have the collection of DOM nodes available to you using jQuery.each. An alternative is to use $.proxy...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.