Created
March 13, 2012 04:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
E.g.
compare = {
attribute: 'customProp',
otherAttr: 'prop'
}
$("#test li").filterByDataset(compare).hide(); // Will hide Element 1