Jquery syntax syntax $ ([]) and $ ("*")

The next line of code is deduced from the official dialog / # variant of the modal form

allFields = $( [] ).add( name ) 

Can someone clarify what $( [] ) does? Is it the same as $("*") ?

Another perplexity is that I did not notice that allFields added / added to any / any object, it is only created and changed. Did I miss something?

+6
source share
4 answers

If you look at doco for the jQuery() function - it usually looks like an abridged version of $() - you will see that it accepts several different types and combinations of parameters.

The syntax you asked for is:

 $([]) 

is the jQuery( elementArray ) syntax that allows you to pass an array of DOM elements, where return will be the jQuery object wrapping these elements. By using an empty array, you basically get an empty jQuery object (as if you would pass a selector string that didn't match anything, but without the inefficiency of trying to find a match in the first place).

When the created allFields contains three DOM elements added to it (where name , email and password are created immediately before as jQuery objects containing each DOM element):

 allFields = $( [] ).add( name ).add( email ).add( password ) 

Presumably the advantage of adding individual elements, rather than just:

 allFields = $("#name,#email,#password") 

lies in the fact that separate objects for each element are also necessary and would be created in any case, so there is no need to worry about re-selecting them using the query string.

Another perplexity is that I did not notice that allFields added / added to any / any object, it has only been created and changed, have I missed something?

It is mentioned in two other places in the code:

 allFields.removeClass( "ui-state-error" ); // and, later allFields.val( "" ).removeClass( "ui-state-error" ); 

This is a pretty standard use of jQuery to remove a class or set the value of all elements in a jQuery object. No need to add it to another object.

+3
source

$([]) creates an empty jQuery object, like $() does. $('*') creates a jQuery object that contains all the elements that match the CSS * selector that will correspond to each element.

In other words, $([]) gets nothing, $('*') gets everything.

+5
source

jQuery (elementArray)

elementArray: an array containing the set of DOM elements to transfer to the jQuery Object.

$( [] ) create an empty set, which is wrapped in a jQuery object.

+1
source

Looks like you mean the following lines:

 var name = $( "#name" ), email = $( "#email" ), password = $( "#password" ), allFields = $( [] ).add( name ).add( email ).add( password ) 

What is happening here is that they have selected three elements, and now they want to create one jQuery object that references all three.

This is the same as the following, except that they avoid selector calls:

  allFields = $("#name, #email, #password") 

In essence, they created a jQuery object with a null element with $([]) , then added a name, email address and password, so the final object was a jQuery object of length 3.

+1
source

Source: https://habr.com/ru/post/907193/


All Articles