Created
July 19, 2013 03:41
-
-
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.
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
/** | |
* 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