Is there a shorter way to write this?
$myelements.find('*').andSelf().filter(myselector)
As far as I can tell, there’s no way to search all descendants, including itself, without doing this?
If there’s no function for this, I’ll write one, but I don’t want to reinvent the wheel if it already exists.
Here it is (revised):
(function($,undefined) {
$.fn.all = function(selector) {
if(selector===undefined) return this.find('*').andSelf();
return this.find(selector).add(this.filter(selector));
}
})(jQuery);
No there isn’t a way to do it in the API, though I think you may get better performance results if you do a normal
.find()with a selector, then apply the.filter()only to the element itself.This way,
querySelectorAllcan be used if you’re using a valid selector.Otherwise, you’re doing the filter entirely with JavaScript code instead of native code (in supported browsers).
Of course you can make it into a plugin as well.