[jQuery] Unwanted side effects when jQuery object is empty?

[jQuery] Unwanted side effects when jQuery object is empty?


> $(".rounded").removeClass("rounded").wrap(env("div", "test",
{"class":"rounded"}));
> I found that the function was being called once
> even though no html was generated.

The jQuery object doesn't have any elements, but by the time the wrap method
executes it has already evaluated its arguments, which in this case includes
your env() function. The env() executes once, only once, and always once, no
matter how many elements the jQuery object has selected.
> I can avoid the problem by rewriting the code as something like
>
$(".rounded").removeClass("rounded").each(function(){$(this).wrap(env("div",
"test", {"class":"rounded"}))});
Right, because the anonymous function will be executed once for each element
in the jQuery object.
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/