public
Last active

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.

  • Download Gist
jquery.ba-addprior.js
JavaScript
1 2 3 4
jQuery.fn.addPrior = function(selector) {
var prior = this.prevObject;
return this.add(selector == null ? prior : prior.filter(selector));
};
usage.js
JavaScript
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")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.