Skip to content

Commit

Permalink
Prevent duplicate ids
Browse files Browse the repository at this point in the history
When having a bigger amount of color pickers on a page it would happen that
multiple pickers had the same id.

By using a counter that increments for each color picker the id is now
guaranteed to be unique.
  • Loading branch information
Christiaan Baartse committed May 6, 2015
1 parent 50866c0 commit fb53628
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion js/colpick.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ For usage and examples: colpick.com/plugin
}
return hex;
},
getUniqueID = (function () {
var cnt = 0;
return function () {
cnt += 1;
return cnt;
};
})(),
restoreOriginal = function () {
var cal = $(this).parent();
var col = cal.data('colpick').origColor;
Expand Down Expand Up @@ -321,7 +328,7 @@ For usage and examples: colpick.com/plugin
var options = $.extend({}, opt);
options.origColor = opt.color;
//Generate and assign a random ID
var id = 'collorpicker_' + parseInt(Math.random() * 1000);
var id = 'collorpicker_' + getUniqueID();
$(this).data('colpickId', id);
//Set the tpl's ID and get the HTML
var cal = $(tpl).attr('id', id);
Expand Down

0 comments on commit fb53628

Please sign in to comment.