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

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -