forked from cowboy/jquery-misc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.ba-getClassData.js
41 lines (32 loc) · 1.12 KB
/
jquery.ba-getClassData.js
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
/*!
* getClassData - v1.1 - 3/30/2009
* http://benalman.com/projects/jquery-misc-plugins/
*
* Copyright (c) 2009 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
// If you're not yet using HTML 5 data- attributes, you can store basic data
// in an element's class attribute for easy retrieval. Just give each datum a
// prefix, which you can then use to select it.
//
// Note: data can't contain spaces, and prefix is case-sensitive.
(function($) {
'$:nomunge'; // Used by YUI compressor.
var jq_getClassData,
str_getClassData = 'getClassData';
$[ str_getClassData ] = jq_getClassData = function( class_string, prefix, delimiter ) {
var re,
arr,
data = [];
delimiter = delimiter || '-';
re = new RegExp( '(?:^|\\s)' + prefix + delimiter + '(\\S+)', 'g' );
while ( arr = re.exec(class_string) ) {
data.push( arr[1] );
}
return data.join(' ');
};
$.fn[ str_getClassData ] = function( prefix, delimiter ) {
return jq_getClassData( $(this).attr('class'), prefix, delimiter );
};
})(jQuery);