This is the "function" of the .add() method. It does not modify the original jQuery object, but returns a new object with an added value.
Given your first example, you need to return the arg variable and overwrite ele .
function test(arg){ arg = arg.add($('<span/>')) console.log(arg); return arg;
EDIT: To give another illustration using your code, but with .push() , which modifies the original object, you will see the correct value updated in ele .
function test(arg){ arg = arg.push($('<span/>')[0]) console.log(arg);
EDIT: After the last illustration. Since .add() returns a new object, you can update both variables to point to the same value as this:
ele = arg = arg.add($('<span/>'));
Now, instead of ele , which refers to the original, and arg , which refer to the new Object that was created, both variables contain a reference to the same object in memory.
user113716
source share