Skip to content

Instantly share code, notes, and snippets.

@jimmyhillis
Created July 19, 2013 03:41
Show Gist options
  • Save jimmyhillis/6034952 to your computer and use it in GitHub Desktop.
Save jimmyhillis/6034952 to your computer and use it in GitHub Desktop.
Indirect allows you to return a list of elements from selectors found within a set of elements from a selector. It's pointers for jQuery, which can be very useful when dealing with filtering and categorizing event actions like showing/hiding relating navigation elements for mobile.
/**
* Indirect returns elements found by the called selector within the
* provided attribute.
*
* > <a href="#" class="show-menu" data-show-navigation=".main-navigation">
* > $('.show-menu').indirect('[data-show-navigation]')
*
* This will return the equivalent of calling $('.main-navigation') which
* allows you to indirectly find a list of elements from a set of
* existing "pointer" elements.
*
* @param {string} attr String name of attribute containing selector
* @return {[]} jQuery list of selector elements
*/
$.fn.indirect = function (attr) {
var $this = $(this);
var selectors = [];
var x = 0;
for (x; x < $this.length; x++) {
selectors.push($(this[x]).attr(attr));
}
return $(selectors.join(','));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment