jQuery object and DOM element

JavascriptDomJquery

Javascript Problem Overview


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 ?

Javascript Solutions


Solution 1 - Javascript

>I would like to understand relationship between jQuery object and DOM element

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.

>Also what methods can operate on jQuery object vs DOM element? Can a single jQuery object represent multiple DOM elements ?

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:

$("selector")[0] // Accesses the first DOM element in this jQuery object
$("selector").get(0) // Equivalent to the code above
$("selector").get() // Retrieve a true array of DOM elements matched by this selector

In other words, the following should get you the same result:

<div id="foo"></div>

alert($("#foo")[0]);
alert($("#foo").get(0));
alert(document.getElementById("foo"));

For more information on the jQuery object, see the documentation. Also check out the documentation for .get()

Solution 2 - Javascript

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:

var jQueryCollection = $("div"); //Contains all div elements in DOM

The above line could be performed without jQuery:

var normalCollection = document.getElementsByTagName("div");

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 the get method:

var div1 = jQueryCollection.get(0); //Gets the first element in the collection

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.

Solution 3 - Javascript

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 wrapping this 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.

Solution 4 - Javascript

Most jQuery member Functions do not have a return value but rather return the current jQuery Object or another jQuery Object.


So,

console.log("(!!) jquery >> " + $("#id") ) ; 

will return [object Object], i.e. a jQuery Object which maintains the collection which is the result of evaluating the selector String ("#id") against the Document,

while ,

console.log("(!!) getElementById >> " + document.getElementById("id") ) ;

will return [object HTMLDivElement] (or in fact [object Object] in IE) because/if the return value is a div Element.


> Also what methods can operate on jQuery object vs DOM element? (1) Can a single jQuery object represent multiple DOM elements ? (2)

(1) There is a host of member Functions in jQuery that pertain to DOM Objects. The best thing to do imo is search the jQuery API documentation for a relevant Function once you have a specific task (such as selecting Nodes or manipulating them).

(2) Yes, a single jQuery Object may maintain a list of multiple DOM Elements. There are multiple Functions (such as jQuery.find or jQuery.each) that build upon this automatic caching behaviour.

Solution 5 - Javascript

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).

Solution 6 - Javascript

Besides what has been mentioned, I'd like to add something about why jQuery object is imported according to description from jquery-object

  • Compatibility

> The implementation of element methods varies across browser vendors and versions.

As an example, set innerHTML of element may not work in most versions of Internet Explorer.

You can set innerHTML in jQuery way and jQuery will help you hide the differences of browser.

// Setting the inner HTML with jQuery.
 
var target = document.getElementById( "target" );
 
$( target ).html( "<td>Hello <b>World</b>!</td>" );
  • Convenience

jQuery provides a list of methods bound to jQuery object to smooth developer's experience, please check some of them under http://api.jquery.com/. The website also provides a common DOM manipulation, let's see how to insert an element stored in newElement after the target element in both way.

The DOM way,

// Inserting a new element after another with the native DOM API.
 
var target = document.getElementById( "target" );
 
var newElement = document.createElement( "div" );
 
target.parentNode.insertBefore( newElement, target.nextSibling );

The jQuery way,

// Inserting a new element after another with jQuery.
 
var target = document.getElementById( "target" );
 
var newElement = document.createElement( "div" );
 
$( target ).after( newElement );

Hope this is a supplement.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncopenndthagenView Question on Stackoverflow
Solution 1 - JavascriptAndrew WhitakerView Answer on Stackoverflow
Solution 2 - JavascriptJames AllardiceView Answer on Stackoverflow
Solution 3 - JavascriptrkwView Answer on Stackoverflow
Solution 4 - JavascriptFK82View Answer on Stackoverflow
Solution 5 - JavascriptHalcyonView Answer on Stackoverflow
Solution 6 - JavascriptEugeneView Answer on Stackoverflow