Search code examples
jqueryjquery-pluginsdefault

JQuery Accessing Variables in a Plugin


How can I access and set variables in a plugin from document ready. I have found some related questions here on Stack Overflow but they didn't help me understand how this can be done.

(function($){
   $.fn.myPlugin = function(){
      var myVar1 = true;
      var myVar2 = true;
      var myVar3 = true;

      ....
      
      if(myVar1 == true){
         // do something
         ...
      }

      ....
   }
})(jQuery);

<script type="text/javascript">
   $(document).ready(function() {
      $.fn.myPlugin.myVar1 = false;
   });
</script>

Solution

  • Use this instead of var:

    this.myVar1 = true;
    this.myVar2 = true;
    
    ....