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() {...
gen_Eric
228k42 gold badges304 silver badges343 bronze badges
asked Oct 16, 2012 at 15:52
Tomas Randus
2,3254 gold badges32 silver badges40 bronze badges
1 Answer 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.
answered Oct 16, 2012 at 16:21
Ed .
6,4139 gold badges64 silver badges85 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Tomas Randus
thank you, it works fine. Is there any way to reach DOM node without sending this by the 'call'.
Ed .
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...lang-js
thisin$(), it's already a jQuery object.thisshould be and what you want to call, in what order?