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

Sean

I'm trying to get write a simple jQuery plugin, I've come to a stumbling block though in that I'm not sure how to access variables on the plugin from within a method for that 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" then "undefined" (so attempting to access it from within the method returns an empty variable), is there any way to access testVar from within the testMethod? Is this not possible? Am I going about this in the wrong way?

Manish Kr. Shukla

You are creating a local variable inside your first method. That makes it's scope restricted to the method itself.

Instead, you should actually create that variable with reference to the plugin like this :

$.fn.testPlugin.testVar = "Hello World";

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I access a field inside a subquery within a subquery?

来自分类Dev

How do I use a Codeigniter method within my own function?

来自分类Dev

How do I access random elements from a vector in R studio?

来自分类Dev

How do i call a controller action from within my view using Razor?

来自分类Dev

Rails: How do I require ActiveSupport's rescue_from method?

来自分类Dev

Swift: How do I return a value within an asynchronous urlsession function?

来自分类Dev

how can I reset a blueimp jQuery fileupload plugin?

来自分类Dev

puppet - How do I append to path variable?

来自分类Dev

How do I install a plugin for gEdit v3?

来自分类Dev

In Puppet, how can I access a variable/attribute inside a defined type?

来自分类Dev

How to call method within builder

来自分类Dev

How do I include jquery in javafx webview?

来自分类Dev

Swift: How do I get access to the navigationController in AppDelegate

来自分类Dev

How do I access variadic template parameters in a way that is constexpr compatible?

来自分类Dev

How do I effectively access list information in C++?

来自分类Dev

Invoking operator[] method from within object

来自分类Dev

How do I mock a method call inside the method I want to test using NUnit?

来自分类Dev

How do I pass async method as Action or Func

来自分类Dev

Object oriented - Method vs. variable access

来自分类Dev

Access class variable from instance

来自分类Dev

How do I redirect the output of a system() function to a variable?

来自分类Dev

How do I turn a function variable into a div id name?

来自分类Dev

How do I use backend variable in ejs script section?

来自分类Dev

jQuery- How to get the GET variable from a URL

来自分类Dev

How to access absolute path to Gruntfile within Gruntfile?

来自分类Dev

jQuery plugin: Do stuff to matched elements

来自分类Dev

how do i consume net.tcp binding within web project?

来自分类Dev

How can I change the selected text when using Multiple Select jQuery plugin

来自分类Dev

PhantomJS; If I set a variable inside page.evaluate(), how can I access the value of that variable outside of page.evaluate()

Related 相关文章

  1. 1

    How do I access a field inside a subquery within a subquery?

  2. 2

    How do I use a Codeigniter method within my own function?

  3. 3

    How do I access random elements from a vector in R studio?

  4. 4

    How do i call a controller action from within my view using Razor?

  5. 5

    Rails: How do I require ActiveSupport's rescue_from method?

  6. 6

    Swift: How do I return a value within an asynchronous urlsession function?

  7. 7

    how can I reset a blueimp jQuery fileupload plugin?

  8. 8

    puppet - How do I append to path variable?

  9. 9

    How do I install a plugin for gEdit v3?

  10. 10

    In Puppet, how can I access a variable/attribute inside a defined type?

  11. 11

    How to call method within builder

  12. 12

    How do I include jquery in javafx webview?

  13. 13

    Swift: How do I get access to the navigationController in AppDelegate

  14. 14

    How do I access variadic template parameters in a way that is constexpr compatible?

  15. 15

    How do I effectively access list information in C++?

  16. 16

    Invoking operator[] method from within object

  17. 17

    How do I mock a method call inside the method I want to test using NUnit?

  18. 18

    How do I pass async method as Action or Func

  19. 19

    Object oriented - Method vs. variable access

  20. 20

    Access class variable from instance

  21. 21

    How do I redirect the output of a system() function to a variable?

  22. 22

    How do I turn a function variable into a div id name?

  23. 23

    How do I use backend variable in ejs script section?

  24. 24

    jQuery- How to get the GET variable from a URL

  25. 25

    How to access absolute path to Gruntfile within Gruntfile?

  26. 26

    jQuery plugin: Do stuff to matched elements

  27. 27

    how do i consume net.tcp binding within web project?

  28. 28

    How can I change the selected text when using Multiple Select jQuery plugin

  29. 29

    PhantomJS; If I set a variable inside page.evaluate(), how can I access the value of that variable outside of page.evaluate()

热门标签

归档