I would like to understand relationship between jQuery object and DOM element..
When jQuery returns an element it shows up as [object Object]
in an alert.
When getElementByID
returns an element it shows up as [object HTMLDivElement]
. What does that mean exactly? I mean are both of them objects with a difference ?
Also what methods can operate on jQuery object vs DOM element? Can a single jQuery object represent multiple DOM elements ?
When you use jQuery to obtain an DOM element, the jQuery object returns contains a reference to the element. When you use a native function like
getElementById
, you get the reference to the element directly, not contained within a jQuery object.A jQuery object is an array-like object that can contain multiple DOM elements:
The above line could be performed without jQuery:
In fact, that's exactly what jQuery will do internally when you pass in a simple selector like
div
. You can access the actual elements within a jQuery collection using theget
method:When you have an element, or set of elements, inside a jQuery object, you can use any of the methods available in the jQuery API, whereas when you have the raw element you can only use native JavaScript methods.
I just barely started playing with jQuery this last month, and I had a similar question running around in my mind. All the answers you have received so far are valid and on the dot, but a very precise answer may be this:
Let's say you are in a function, and to refer to the calling element, you can either use
this
, or$(this)
; but what is the difference? Turns out, when you use$(this)
, you are wrappingthis
inside a jQuery object. The benefit is that once an object is a jQuery object, you can use all the jQuery functions on it.It's pretty powerful, since you can even wrap a string representation of elements,
var s = '<div>hello <a href='#'>world</a></div><span>!</span>'
, inside a jQuery object just by literally wrapping it in $():$(s)
. Now you can manipulate all those elements with jQuery.That's just your browser being clever. They're both objects but DOMElements are special objects. jQuery just wraps DOMElements in a Javascript object.
If you want to get more debug info I recommend you look at debugging tools like Firebug for Firefox and Chrome's built-in inspector (very similar to Firebug).
Most jQuery member
Functions
do not have a return value but rather return the currentjQuery Object
or anotherjQuery Object
.So,
will return
[object Object]
, i.e. ajQuery Object
which maintains the collection which is the result of evaluating the selectorString
("#id"
) against theDocument
,while ,
will return
[object HTMLDivElement]
(or in fact[object Object]
in IE) because/if the return value is adiv
Element
.(1) There is a host of member
Function
s in jQuery that pertain to DOMObject
s. The best thing to do imo is search the jQuery API documentation for a relevantFunction
once you have a specific task (such as selectingNode
s or manipulating them).(2) Yes, a single
jQuery Object
may maintain a list of multiple DOMElement
s. There are multipleFunctions
(such asjQuery.find
orjQuery.each
) that build upon this automatic caching behaviour.A jQuery object is an array-like object that contains DOM element(s). A jQuery object can contain multiple DOM elements depending on the selector you use.
jQuery functions (a full list is on the website) operate on jQuery objects and not on DOM elements. You can access the DOM elements inside a jQuery function using
.get()
or accessing the element at the desired index directly:In other words, the following should get you the same result:
For more information on the jQuery object, see the documentation. Also check out the documentation for
.get()