JQuery re-query DOM when calling $ (this)?

In the following code, when $(this) is called, does jQuery re-query the DOM as if a selector was passed to it (using some object property as a selector), or does jQuery save the previously returned object?

 $('.someButton').on('click', function() { $(this).remove(); // Is this another lookup, or just a wrapper for the previously returned object? }); 
+6
source share
1 answer

It does not re-query the DOM, this already an element. jQuery simply sets the context for the element, adjusts the length, and returns itself. This code is from the init function, which executes when you execute $(something) , this is part of the big if..else , where it also checks selectors, arrays by the way:

 // HANDLE: $(DOMElement) } else if (selector.nodeType) { this.context = this[0] = selector; this.length = 1; return this; 

So basically it just wraps the element in a new jQuery object.

+5
source

All Articles