Tomas Randus
Tomas Randus

Reputation: 2285

jQuery plugin call function from function

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() {...

Upvotes: 0

Views: 196

Answers (1)

Ed .
Ed .

Reputation: 6403

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.

Upvotes: 1

Related Questions