Last active
December 16, 2015 21:40
-
-
Save adam-lynch/5501843 to your computer and use it in GitHub Desktop.
A jQuery function which returns a subset of the given element(s) (jQuery object), where each element returned has the data attribute specified and thatdata attribute's value is equal to that of the second parameter (if one was given)
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
/** | |
* @param dataAttributeName string | |
* @param desiredDataAttributeValue string (optional) | |
* (desiredDataAttributeValue must be a string, strict comparison is done with data attribute value) | |
* | |
* @author: adam-lynch | |
*/ | |
$.fn.filterByDataAttribute = function(dataAttributeName, desiredDataAttributeValue){ | |
return $(this).filter(function() { | |
return desiredDataAttributeValue && $(this).data(dataAttributeName) === desiredDataAttributeValue | |
|| !desiredDataAttributeValue && $(this).is('*[data-'+dataAttributeName+']'); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment