Skip to content

Instantly share code, notes, and snippets.

@tehsis
Created March 13, 2012 04:59
Show Gist options
  • Save tehsis/2026875 to your computer and use it in GitHub Desktop.
Save tehsis/2026875 to your computer and use it in GitHub Desktop.
jQuery plugin which filter elements acording its data in its dataset and a passed object
(function ($) {
$.fn.filterByDataset = function(compare) {
return this.filter( function() {
if ($.fn.dataset) {
var attributes = $(this).dataset();
} else {
var attributes = this.dataset;
}
var match = true;
for (attribute in compare) {
var attribute_array = attributes[attribute].split(",");
var i;
for(i=0; i<compare[attribute].length; i++) {
if (attribute_array.indexOf(compare[attribute][i]) === -1){
match = false;
}
}
}
return match;
});
}
}) (jQuery);
@tehsis
Copy link
Author

tehsis commented Mar 13, 2012

E.g.

  • Element 1
  • Element 2
  • Element 3

compare = {
attribute: 'customProp',
otherAttr: 'prop'
}
$("#test li").filterByDataset(compare).hide(); // Will hide Element 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment