Barring someone being able to point to an article by John Resig specifically addressing the question, I doubt a definitive answer is on offer. But I frequently wondered this when I started using jQuery, and have come to the conclusion that it's the least harmful thing. After all, it's entirely possible that all you need is the DOM element. If you need/want to use a jQuery wrapper around it, then $()
is but three keystrokes away (or more of you do the var $this = $(this);
thing). As opposed to having the library always do it for you, which involves multiple function calls and memory allocations, when it may well be that you don't need it.
For instance, consider:
$("input").each(function() {
// Use this.value
});
There, there's no reason whatsoever for a jQuery wrapper around the DOM element. this.value
is all you need for all input
fields (other than input type="file"
, but val()
won't help you there, either). In event handlers (where this
is also "just" the raw element), you may not look at this
at all.
But in this case:
$(":input").each(function() {
var val = $(this).val();
// Use `val`
});
...there's a genuine reason for using $(this)
: The :input
selector matches several different element types (input
, select
, button
, textarea
) for which the value comes from different properties, and val()
abstracts that for you.
So I've concluded that providing the raw element is a performance thing. If you don't use it at all, or you only use the basic DOM properties of it, there's no need to wrap it in a jQuery instance. If you need/want to do that, you can do it in your code.