JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<div id="entry1">
x
</div>
<div id="entry2">
y
</div>
</body>
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) {
return $.each(this, function() {
var currentElement = $(this);
if(currentElement.data(CONST_TEST_KEY))
return;
currentElement.data(CONST_TEST_KEY, new TestObject(currentElement, {}));
});
};
})(jQuery);
$(document).ready(function() {
var object1 = $("#entry1").testJQueryPlugin();
var object2 = $("#entry2").testJQueryPlugin();
var o1 = $(object1);
var o2 = $(object2)
alert(o1.data(CONST_TEST_KEY).random);
alert(o2.data(CONST_TEST_KEY).random);
});