javascript - How do I access a jQuery plugin variable from within method? -


i'm trying write simple jquery plugin, i've come stumbling block though in i'm not sure how access variables on plugin within method plugin.

(function($) {     $.fn.testplugin = function() {         var testvar = "hello world";         alert(testvar);         $.fn.testplugin.testmethod();     };      $.fn.testplugin.testmethod = function() {         alert($.fn.testplugin.testvar);     }; }(jquery));  $("body").testplugin(); 

fiddle here

this code first alerts "hello world" "undefined" (so attempting access within method returns empty variable), there way access testvar within testmethod? not possible? going in wrong way?

you creating local variable inside first method. makes it's scope restricted method itself.

instead, should create variable reference plugin :

$.fn.testplugin.testvar = "hello world"; 

your updated plunker goes here : http://jsfiddle.net/5zt9ps20/


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -