Hi Alex, Sorry for the short response earlier. Functions have 'apply' and 'call' methods which you can use to control the value of 'this' when the function executes. So in my example, I invoke onIni by calling its apply method, and I pass it 'this' which at the time is a DOM element, so when onIni executes 'this' is that same DOM element.
Now you actually had several problems in that plugin code you posted: $.fn.myplugin= function(options,callback){ return this.each(function(index){ $this = $(this); uniq = 'iAmNumber_' + index; $this.options.onIni(); }); } For starters, that code creates global variables named $this and uniq because you forgot the var keyword. Remember to always use var to scope your variables. Next, you're trying to access the 'options' property of the $this object. $this is a jQuery object and it doesn't have an options property so I would expect a script error on that line. The options object is in scope because it is passed into the plugin function so you just access it directly. But if you invoke onIni like this: options.onIni() then when onIni executes 'this' will be the options object, which is not what you want. To set 'this' we use apply: options.onIni.apply(this) You can set up 'this' to be whatever you want, but what your onIni function expects to find there is a DOM element so that's what you should pass to apply. Hope this helps. There are lots of online resources that can explain it much better than I can. I'm no Michael Geary! Mike On Fri, Feb 15, 2008 at 4:26 PM, Alexandre Plennevaux <[EMAIL PROTECTED]> wrote: > > Well, it's now tested, and it works!! > > I'm very sorry for this probably stupid question, i'm learning > javascript from scratches. Could you explain or point me to the theory > behind this? > > thank you so much > > alex > > > On Feb 15, 10:14 pm, "Mike Alsup" <[EMAIL PROTECTED]> wrote: > > Alexandre, > > > > This is untested, but I think it's closer to what you're looking for: > > > > $.fn.myplugin= function(options,callback) { > > return this.each(function(index) { > > this.uniq = 'iAmNumber_' + index; > > options.onIni.apply(this); > > }); > > > > } > > > > On Fri, Feb 15, 2008 at 4:00 PM, Alexandre Plennevaux < > [EMAIL PROTECTED]> > > wrote: > > > > > > > > > hey mates, > > > > > I didn't find an answer to my week-old question so i'll rephrase to > > > simplify and focus on just the little bit of knowledge i need from you > > > guys: > > > > > say i want to create a function that performs some jquery methods on > an > > > element. > > > > > function doThis(){ > > > $(this).text('i've been changed'); > > > alert("my uniq="+this.uniq); > > > } > > > > > now, inside a plugin, i want to use that function as an option value > > > > > $('div.toBeSelected').myplugin({onIni:doThis}); > > > > > here is my pseudo plugin: > > > > > $.fn.myplugin= function(options,callback){ > > > return this.each(function(index){ > > > $this = $(this); > > > uniq = 'iAmNumber_' + index; > > > $this.options.onIni(); > > > }); > > > } > > > > > So far, the function is called, but the alert returns "my uniq = > > > undefined" and the text() command is not performed. Why, oh why ? > > > > > -- > > > Alexandre Plennevaux > > > LAb[au] > > > > >http://www.lab-au.com >