-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery.data.filter.js
37 lines (34 loc) · 1.09 KB
/
jquery.data.filter.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
(function($) {
$.fn.dfilter = function(attribute, value) {
attribute = attribute.replace(/^data-/, "");
if (typeof value === "undefined" || value === "") {
return this.filter('[data-' + attribute + ']');
}
else if (typeof value === "boolean") {
if (value) { // value === true
return this.filter('[data-' + attribute + ']')
}
else { // value === false
return this.not('[data-' + attribute + ']');
}
}
else if ($.isArray(value)) {
// In the case that we have an array, we want to build a big multiple selector string:
// If we feed in something like:
//
// [100, 101]
//
// Then we build the following selector:
//
// [data-id="100"],[data-id="101"]
//
// For more reading on the multiple selector, see
//
// http://api.jquery.com/multiple-selector/
return this.filter(
'[data-' + attribute + '="' + value.join('"],[data-' + attribute + '="') + '"]'
);
}
return this.filter('[data-' + attribute + '="' + value + '"]');
};
})(jQuery);