Why return this.each(function()) in jQuery plugins?
Solution 1:
When you filter elements with a selector ($('.myclass')
), it can match more than only one element.
With each
, you iterate over all matched elements and your code is applied to all of them.
Solution 2:
jQuery supports "chainable methods", which means that most jQuery functions should return this
. .each()
returns this
, and if you want $('selector').yourPlugin().css(...)
to work, you should return this
.