jquery-next-id
Version:
jQuery collection plugin that will assign the next ID in sequence if an ID property does not already exist.
2 lines (1 loc) • 450 B
JavaScript
(function($,window,document,undefined){var _nextInSequenceMap={};$.fn.nextId=function nextId(prefix){prefix=prefix||$.fn.nextId.defaults.prefix;_nextInSequenceMap[prefix]=_nextInSequenceMap[prefix]||0;return this.filter(function onFilter(){return!this.id;}).each(function onEach(){$(this).prop('id',prefix+$.fn.nextId.defaults.separator+_nextInSequenceMap[prefix]++);});};}(jQuery,window,document));$.fn.nextId.defaults={prefix:'nid',separator:'-'};