Is there any difference in jQuery ("sel1"). Add ("sel2") and jQuery ("sel1, sel2")

Well, the question is in the name. Is there a difference (performance, caveats) between multiple selectors

jQuery("selector1, selector2") 

and adding items to the selection using add :

 jQuery("selector1").add("selector2") 
+4
source share
1 answer

Yes, the first one will create a jQuery object containing the elements matched by both selectors. The second will create an object in which there are elements corresponding to the first selector, then create and return a new object with both, without changing the first object.

For instance:

 var jq1 = $('h1, h2'); // Will contain all <h1> and <h2> elements. jq1.add('h3'); alert(jq1.filter('h3').length); // Will alert 0, because the // original object was not modified. jq1 = jq1.add('h3'); alert(jq1.filter('h3').length); // Will alert the number of <h3> elements. 
+4
source

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


All Articles