1

Using the jQuery UI Widget Factory, how do I set a property that holds the same value across all instances of the plugin? (So that if the value is changed in one instance, it is changed across all instances.)

eg

$.widget( "myplugin" , { 

   avalue : 1,

});
1
  • That right there should work since it's a global declaration. You may want to namespace your globals though to prevent collisions. Commented Dec 21, 2012 at 17:19

1 Answer 1

1

One way to do this would be to store the value in a closure like this:

(function () {
    var value = 0;

    $.widget('my.plugin', {
        _create: function () {
            value++;
        },
        getValue: function () {
            return value;
        }
    });
})();

$('.element').plugin();
$('.second-element').plugin();

$('.element').plugin('getValue'); // 2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.