The general answer to anything like this in JavaScript is "use a closure."
In fact, you're already doing that in your code, where you have "var self = this;". So you can simply take your clickHandler function and not make it a method of anything (because it won't be called as a method anyway) - instead make it an ordinary function nested inside your init() method. With that change, your code would look like: (function($) { $.widget("ui.test", { init: function() { var self = this; self.element.find('li').each(function(i) { console.log(self); }); self.element.find('li').bind('click', clickHandler); function clickHandler(evt) { console.log(self); } } }); })(jQuery); (I also changed the existing uses of "this" to "self" for consistency - it works the same either way, but I like seeing the same name instead of two different ones.) Of course, at this point you may as well make clickHandler an anonymous function: (function($) { $.widget("ui.test", { init: function() { var self = this; self.element.find('li').each(function(i) { console.log(self); }); self.element.find('li').bind('click', function(evt) { console.log(self); }); } }); })(jQuery); -Mike > From: Bramus! > > Good evening y'all (Europe here ;)), > > been fiddling around in jQuery.UI this afternoon in order to > write a widget. One problem I encountered and couldn't get > properly solved was with referring to the instance of the > widget from within a click event. > > I've set up a demoscript over at > http://www.bram.us/sandbox/jquery/ui/widget.html > Just click the li's and watch your Firebug console ;) > > One path I've walked in order to get it work is to attach > this (the instance of the widget) via $.data on each li. > However, when working with a huge set, that seemed to hog my > system, as Firefox would end up eating over 500MB of RAM. > > Anyone has any clue to this one? Looked at several sources of > the UI core files (draggable, tabs, etc.) and I think they > fall back on the .call() method. Whilst looking at the code > however I couldn't find where exactly the magic happened in > order for it to work. > > Regards, > Bramus! >