Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

jQuery each2: fixed a pretty major bug. Should *actually* work now.

  • Loading branch information...
commit 3641ec9efb37b9d59f335d5482d556a7e389297b 1 parent df199e2
@cowboy cowboy authored
Showing with 20 additions and 5 deletions.
  1. +18 −3 jquery.ba-each2.js
  2. +2 −2 jquery.ba-each2.min.js
View
21 jquery.ba-each2.js
@@ -1,5 +1,5 @@
/*!
- * jQuery each2 - v0.1 - 8/02/2010
+ * jQuery each2 - v0.2 - 8/02/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Inspired by James Padolsey's quickEach
@@ -12,11 +12,26 @@
(function($) {
- var jq = $([]);
+ // Create a placeholder jQuery object with a length of 1. The single item
+ // is completely arbitrary and will be replaced.
+ var jq = $([1]);
$.fn.each2 = function( fn ) {
var i = -1;
- while ( ( jq[0] = this[++i] ) && fn.call( jq[0], i, jq ) !== false ) {}
+
+ while (
+ // Set both the first element AND context property of the placeholder
+ // jQuery object to the DOM element. When i > this.length, this[++i]
+ // will return undefined and abort the while loop.
+ ( jq.context = jq[0] = this[++i] )
+
+ // Invoke the callback function in the context of the DOM element,
+ // passing both the index and the placeholder jQuery object in. Like
+ // .each, if the callback returns `false`, abort the while loop.
+ && fn.call( jq[0], i, jq ) !== false
+ ) {}
+
+ // Return the initial jQuery object for chainability.
return this;
};
View
4 jquery.ba-each2.min.js
@@ -1,5 +1,5 @@
/*
- * jQuery each2 - v0.1 - 8/02/2010
+ * jQuery each2 - v0.2 - 8/02/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Inspired by James Padolsey's quickEach
@@ -9,4 +9,4 @@
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
-(function(a){var b=a([]);a.fn.each2=function(d){var c=-1;while((b[0]=this[++c])&&d.call(b[0],c,b)!==false){}return this}})(jQuery);
+(function(a){var b=a([1]);a.fn.each2=function(d){var c=-1;while((b.context=b[0]=this[++c])&&d.call(b[0],c,b)!==false){}return this}})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.