cycling jQuery_Succinctly | Page 86

alert(this[0].id); // Alerts "counter1". }; })(jQuery); jQuery('#counter1').count(); It is critical that you grok exactly what the keyword this is referring to in the plugin function. Using each() to iterate over the jQuery object and provide a reference to each element in the object using the this keyword Using each(), we can create an implicit iteration for our plugin. This means that if the wrapper set contains more than one element, our plugin method will be applied to each element in the wrapper set. To do this, we use the jQuery utility each() function, which is a generic iterator for both objects and arrays, basically simplifying looping. In the code example below, we use the function to iterate over the jQuery object itself. Inside of the function that is passed to each(), the keyword this will refer to the elements in the jQuery wrapper set. Sample: sample85.html