Obviously it\'s a good idea to store jQuery selectors in variables if they are used more than once (not a good idea if used only once).
My question is, how do y
a selector is a string when passed to the $() fn. returns a collection of Elements that match the selector. So there is nothing wrong with this code.
Your code can also be written as,
var object1 = "#object1",
object2 = "#object2";
$(object1).add(object2).doSomething();
or it can be further optimized as,
var object1 = $('#object1'),
object2 = $('#object2');
object1.add(object2).doSomething();
where doSomething
can be a jQuery or jQuery plugin defined method.