i'm trying to write my first simple jquery plugin, but i don't know how to create private variables inside jquery object. here is code
var first;
var second;
$(document).ready(function(){
first = $("#clicker1").demSelector();
second = $("#clicker2").demSelector();
});
$(".totalClicks").click(function(){
alert("Clicks 1 = "+first.number());
alert("Clicks 2 = "+second.number());
});
(function($){
var num = 0;
$.fn.demSelector = function() {
return this.each(function(){
init(this);
});
};
$.fn.number = function(){
return num;
};
function init(control){
$(control).on('click','.num-of-clicks', function(){
num = parseInt($(this).html(),0) + 1;
$(this).html(num);
});
}
})(jQuery);
and demo project http://jsbin.com/afEduKI/9/edit
When you click "0" it increments and changes variable num. But it changes num for first and second object. So after two clicks on first row and clicking button "Show" i get two messages "Clicks 1 = 2" and "Clicks 2 = 2", but it should be "Clicks 1 = 2" and "Clicks 2 = 0". It looks like that variable num is global.
What am i doing wrong?