Skip to content

Instantly share code, notes, and snippets.

@wombleton
Created February 5, 2010 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wombleton/295320 to your computer and use it in GitHub Desktop.
Save wombleton/295320 to your computer and use it in GitHub Desktop.
jquery generateId that preserves existing ids and guesses appropriate prefixes, similar to Ext's id()
$.extend((function() {
var counts = {};
return {
generateId: function(prefix) {
prefix = (prefix || 'jq') + '-';
counts[prefix] = counts[prefix] || 0;
return prefix + counts[prefix]++;
}
}
})());
$.fn.generateId = function() {
return this.each(function() {
this.id = this.id || $.generateId((this.className.split(' ').shift() || this.tagName.toLowerCase()));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment