jQuery variables and scope
- by Peuge
I am writing a jQuery plugin and am running into a few problems with regard to variables. For example I have the following skeleton structure for my plugin, which is going to act on a textbox. In my init function I want to set a variable and bind a keypress event to my textbox. That keypress event needs to call another function, in which I need the variable. Is this possible?
(function($){
var methods = {
init : function(options){
return this.each(function(){
var $this = $(this);
var someVar = 'somevar';
$this.keypress(function(event){
//I want to call my customFunction
});
});
},
customFunction : function(){
//I am wanting to access 'someVar' here
};
$.fn.mynamespace = function(method){
//handle which method to call here
};
})(jQuery);
Many thanks in advance.