Skip to content

Instantly share code, notes, and snippets.

@davidmars
Created August 26, 2014 09:59
Show Gist options
  • Save davidmars/ede0c77f3e8422961658 to your computer and use it in GitHub Desktop.
Save davidmars/ede0c77f3e8422961658 to your computer and use it in GitHub Desktop.
/**
* For slackers... Quick methos selectors pluggin dealing with attribute names and values.
* @author David Marsalone
* @example $(".myslector").findByAttr("my-attribute-name","my attribute value");
* @example $(".myslector").filterByAttr("my-attribute-name","my attribute value");
*/
(function ( $ ) {
/**
* Quick method to get the descendants of each element in the current set of matched elements, filtered by an attribute name and its value
* @param {string} attributeName The attribute name you are looking for.
* @param {string} attributeValue The attribute value you are looking for.
* @returns {jQuery}
*/
$.fn.findByAttr = function(attributeName,attributeValue) {
return this.find("["+attributeName+"='"+attributeValue+"']");
};
/**
* Reduce the set of matched elements to those that match the given attribute name and its value
* @param {string} attributeName The attribute name you are looking for.
* @param {string} attributeValue The attribute value you are looking for.
* @returns {jQuery}
*/
$.fn.filterByAttr = function(attributeName,attributeValue) {
return this.filter("["+attributeName+"='"+attributeValue+"']");
};
}( jQuery ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment