Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $([]) mean in jQuery?

var username = $("#username"),
                password = $("#password"),
                allFields = $([]).add(username).add(password);

What is allFields? What is $([])?

Being a newbie to Javascript/jQuery, I've never seen this $([]) notation before and I'm interested in its associated methods.

Given that its "$([])", it's tricky to search for. And a Google search of arrays in Javascript (guessing that thing is an array of some sort) yields the typical arrays I'm familiar seeing.

So what is $([])? Can anyone point me to some documentation? I'm interested in learning how to use this strange thing.

like image 999
Tina D. Avatar asked Aug 27 '10 19:08

Tina D.


People also ask

What is the $() in jQuery ()?

When a jQuery object is passed to the $() function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.

What does $this mean in jQuery?

this is a reference to the html DOM element that is the source of the event. $(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this .

What does $() mean in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.

What does ++ mean in jQuery?

The ++ notation is the increment operators. i++ is the same thing of. i = i +1.

What is jQuery and how to use it?

With jQuery you select (query) HTML elements and perform "actions" on them. The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element (s).

What does the $ sign mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery (), then an alias to a function. The jQuery syntax is tailor made for selecting HTML elements and perform some action on the element (s). A dollar sign to define jQuery A (selector) to "query (or find)" HTML elements A jQuery action () to be performed on the element (s)

What does q mean in jQuery and JavaScript?

In JavaScript it has no special significance (no more than a or Q anyway). It is just an uninformative variable name. In jQuery the variable is assigned a copy of the jQuery function. This function is heavily overloaded and means half a dozen different things depending on what arguments it is passed.

What is the basic syntax for jQuery action?

Basic syntax is: $(selector).action() A $ sign to define/access jQuery A (selector) to "query (or find)" HTML elements A jQuery action() to be performed on the element(s)


4 Answers

The jQuery function accepts an array of DOM nodes.

$([document.body]) for example which will return a jQuery object by wrapping all the DOM elements passed in that array. However, since in your example there is no DOM object to begin with and it's just an empty array, there is not need to even pass an empty array.

Calling the jQuery function without any arguments returns an empty set. Taken from jQuery docs,

Returning an Empty Set

As of jQuery 1.4, calling the jQuery() method with no arguments returns an empty jQuery set. In previous versions of jQuery, this would return a set containing the document node.

So, your example would work the same if you had instead called

$().add(username).add(password);

As other answers have mentioned and the docs say, passing an empty array was required before v 1.4.

like image 65
Anurag Avatar answered Oct 05 '22 19:10

Anurag


[] it's an empty array. Wrapping it with $() overcharges it with jQuery's functions.

In javascript you can create an array this way:

var foo = [];
like image 37
fcingolani Avatar answered Oct 05 '22 19:10

fcingolani


It is an empty array being passed in the creation of a jQuery object. Presumably to initialize something.

Shouldn't be necessary, at least not in the latest versions of jQuery.

Here's an example without the Array: http://jsfiddle.net/MAzLN/

Works fine, as the alert() shows one element in the object.

jQuery initializes the length property to 0 when the object is created, so I'm not sure what the purpose is, unless it was required in the past.

like image 37
user113716 Avatar answered Oct 05 '22 18:10

user113716


I believe it creates an empty jQuery collection object.

like image 35
Cristian Sanchez Avatar answered Oct 05 '22 20:10

Cristian Sanchez