Created
July 11, 2011 19:19
-
-
Save cowboy/1076570 to your computer and use it in GitHub Desktop.
jQuery addPrior: Add the previous set's elements (before the last filter / traversal) to the current set, optionally filtering them. Supersedes jQuery's built-in .andSelf method.
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
jQuery.fn.addPrior = function(selector) { | |
var prior = this.prevObject; | |
return this.add(selector == null ? prior : prior.filter(selector)); | |
}; |
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
// BASIC | |
// select UL + all LI children: | |
$("ul").children("li").addPrior() | |
// MORE COMPLEX | |
// select both first and last children: | |
$("li").filter(":first-child").addPrior(":last-child") | |
// FIND+FILTER | |
var elems = $(something); | |
// lame: | |
elems.find("li").add(elems.filter("li")) | |
// sexy: | |
elems.find("li").addPrior("li") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See jQuery ticket #9800 as well.