jquery add function doesn't work as expected

The following snippet doesn't work.

var empty = $();
var divs = $("div");
empty.add(divs);

There is a div element in the HTML and it is added correctly to divs. But the divs collection is not added to the empty jquery object. Any ideas what`s wrong with that?

Answers


.add won't change the original object. Try:

empty = empty.add(divs);

You can do

var empty = $.extend($(), $('div'));

Per Jquery doc,

The following will not save the added elements, because the .add() method creates a new set and leaves the original set in pdiv unchanged:

var pdiv = $("p");
pdiv.add("div");  // WRONG, pdiv will not change

Need Your Help

Cordova + Angularjs + Device Ready

angularjs cordova angularjs-service

I am developing a mobile application using Cordova and AngularJS. How do I restrict bootstrapping of AngluarJS before Cordova device ready. Basically I don't want to use any of AngularJS controllers

What is the difference between login and credential in SQL server 2008?

sql-server login credentials

I am a bit confused SQL server windows login and credential. On books online, it says that credential can access resources beyond sql server, but as far as i see windows login might be able to do t...