What is jQuery Object

jQuery object contains a collection of DOM elements and behaves like a special array.

An empty jQuery object is nothing but containing “no DOM elements” .

 

There are many element selector used in jQuery. The simplified selection syntax can be as –

jQuerySimpleSelector.png

If our supplied expression (with or without criteria) to find out the matching elements from DOM doesn't find any element(s) then jQuery returns an empty jQuery-object.

Also, an empty Object can be created as –

   var emptyJqueryObject = $([]);

Note: Don't be confused that if you wouldn't supply any selector then () will return you an empty object. jQuery() or $() or $(“”) expects a context and if we do not supply any context, ‘document' is considered as the default context. You can check it as-

       var c = $();

alert( $.isEmptyObject(c) );

       ================= 

Output : False

A jQuery object contains a collection of DOM elements and when filtered using some selectors, this object contains “matched-elements” or “selected elements”. This jQuery object behaves like an array (special array and not like native JavaScript array, we can use length but not join() on it). In addition to length, we can access elements using numeric indices starting from 0 to length-1. Here length is the length of this jQuery object. jQuery object can be created with help of jQuery() or its alias $().

Many jQuery methods return the jQuery object itself (except some destructive methods like find() and filter() that change the object), so that method calls can be easily chained and minimize the code line. Example –

  $('div').css ('color','red').find ('.myCSSname').css ('color','yellow');

The above jQuery code will find all DIV elements on document and apply the css and then inside it find those elements whose class is “myCSSname” and apply the css on this result set. In JavaScript, we have to write it in two steps as there is no chaining. For chaining you can refer my other article on Chaining