Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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.

View jquery.ba-addprior.js
1 2 3 4
jQuery.fn.addPrior = function(selector) {
var prior = this.prevObject;
return this.add(selector == null ? prior : prior.filter(selector));
};
View jquery.ba-addprior.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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")
Owner
cowboy commented

See jQuery ticket #9800 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.