forked from jamierumbelow/jquery.unique-element-id
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.unique-element-id.js
More file actions
57 lines (49 loc) · 1.24 KB
/
jquery.unique-element-id.js
File metadata and controls
57 lines (49 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* jquery.unique-element-id.js
*
* A simple jQuery plugin to get a unique ID for
* any HTML element
*
* Usage:
* $('some_element_selector').uid();
*
* by Jamie Rumbelow <jamie@jamierumbelow.net>
* http://jamieonsoftware.com
* Copyright (c)2011 Jamie Rumbelow
*
* Licensed under the MIT license (http://www.opensource.org/licenses/MIT)
*/
(function($){
var __uid_counter = 0;
/**
* Generate a new unqiue ID
*/
function generateUniqueId() {
// Increment the counter
__uid_counter++;
// Return a unique ID
return "element-" + __uid_counter;
}
/**
* Get a unique ID for an element, ensuring that the
* element has an id="" attribute
*/
$.fn.uid = function(){
// We need an element! Check the selector returned something
if (!this.length > 0) {
return false;
}
// Act on only the first element. Also, fetch the element's ID attr
var first_element = this.first(),
id_attr = first_element.attr('id');
// Do we have an ID?
if (!id_attr) {
// No? Generate one!
id_attr = generateUniqueId();
// And set the ID attribute
first_element.attr('id', id_attr);
}
// Return it
return id_attr;
};
})(jQuery);