Question

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,

});
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top