Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery ui $([])

This is just a quick one (I hope).

I've recently been on to the jquery ui dialog pages to look at using form inside a dialog http://jqueryui.com/dialog/#modal-form and I noticed some code that I hadn't seen before:

var allFields = $([]).add(#input1).add(input2).add(input3)....

I was just wondering what $([]) meant?

At first I thought it was a way to create an array in jquery but when I try:

allFields.add(input4)

after the variable is declared nothing else is being added?!?

Any help would be greatly appreciated! :) I've tried googling this, however, I'm not having much luck.

like image 780
Rwd Avatar asked Oct 04 '22 18:10

Rwd


1 Answers

The jQuery() function accepts a number of different arguments, one of which is an array of DOM elements. Passing such an array will result in a new jQuery object, containing those elements, being returned. In this case they're passing an empty array, so it returns a jQuery object containing zero elements.

The .add() function will add more elements to an existing set.

So $([]) creates a jQuery object containing no elements, .add('#input1') would add the element with an id of input1 to that set. Calling .add('#input1') again would add nothing because the element is already present.

like image 171
Anthony Grist Avatar answered Oct 10 '22 03:10

Anthony Grist