jQuery variables and scope
        Posted  
        
            by 
                Peuge
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Peuge
        
        
        
        Published on 2011-01-17T15:33:34Z
        Indexed on 
            2011/01/17
            15:53 UTC
        
        
        Read the original article
        Hit count: 283
        
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.
© Stack Overflow or respective owner