Skip to content

Instantly share code, notes, and snippets.

@samshull
Last active October 12, 2015 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samshull/4026233 to your computer and use it in GitHub Desktop.
Save samshull/4026233 to your computer and use it in GitHub Desktop.
Iterative jQuery methods that use a single jQuery object for context to simplify usage
(function($){
$.fn.$each = function(callback) {
var $temp = $([1]);
return this.each(function() {
$temp[0] = this;
return callback.apply($temp, arguments);
});
};
$.fn.$filter = function(callback, invert) {
var $temp = $([1]);
return this.filter(function() {
$temp[0] = this;
return callback.apply($temp, arguments);
}, invert);
};
$.fn.$map = function(callback) {
var $temp = $([1]);
return this.map(function() {
$temp[0] = this;
return callback.apply($temp, arguments);
});
};
$.fn.some = $.fn.is;
$.fn.$some = $.fn.$is = function() {
return this.$filter.apply(this, arguments).length > 0;
};
$.fn.every = function() {
return this.length === this.filter.apply(this, arguments).length;
};
$.fn.$every = function() {
return this.length === this.$filter.apply(this, arguments).length;
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment