Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created July 11, 2011 19:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/1076570 to your computer and use it in GitHub Desktop.
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.
jQuery.fn.addPrior = function(selector) {
var prior = this.prevObject;
return this.add(selector == null ? prior : prior.filter(selector));
};
// 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")
@cowboy
Copy link
Author

cowboy commented Jul 11, 2011

See jQuery ticket #9800 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment