StackOverflow Q4103623
http://stackoverflow.com/questions/4103623/custom-jquery-plugin-exposing-methods-and-storing-element-information/4103687#4103687
by Matt Ball
HTML
<div id="entry1">x</div>
<div id="entry2">y</div>
JavaScript
var CONST_TEST_KEY = "test-data-key";
(function($) {
var TestObject = function(e, o) {
var elem = $(e);
this.random = Math.floor(Math.random() * 100);
this.GetRandom = function() {
alert(this.random);
}
};
$.fn.testJQueryPlugin = function(o) {
$(this).each(function() {
var currentElement = $(this);
if (!currentElement.data(CONST_TEST_KEY)) {
currentElement.data(CONST_TEST_KEY, new TestObject(currentElement, {}));
}
});
return this;
};
}(jQuery));
alert($("#entry1").testJQueryPlugin().data(CONST_TEST_KEY).random);
alert($("#entry2").testJQueryPlugin().data(CONST_TEST_KEY).random);