1

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 you store multiple selectors, that are used interchangeably, in a variable.

For instance, let's say I select $('#object1, #object2), then later on I select `$('#object1'). How would I create a variable that can be combined with other variables to create multiple selectors.

If I were to write it like this:

var object1 = "#object1";
var object2 = "#object2";

$(object1  + "," + object2);

I would only be referencing the string #object1, and not the actual selector.

So how do I store multiple selectors in variables, so they can be used interchangeably?

3
  • What's wrong with that code? I think it would work just fine. Commented Jan 27, 2011 at 15:24
  • it's storing just a string as a var, not the actual selector. Commented Jan 27, 2011 at 15:38
  • 2
    To be precise, the selector is the string, while what you're referring to is a jQuery object. Commented Jan 27, 2011 at 15:48

5 Answers 5

8

The problem is not the selector string, the problem is the query of the DOM element. "Problem" means, it's expensive. That is why you should only query an element once and store a reference to it.

var object1 = $('#object1'),
    object2 = $('#object2');

object1.some_method();

update

In reference to your comment:

jQuery offers the .add()help method which allows to concat jQuery objects:

object1.add(object2).method();
Sign up to request clarification or add additional context in comments.

1 Comment

That's easy enough to do, but how would you apply some_method(); to #object1 and #object2 ?
3

Not completely sure I understand what you mean. You want to store the jQuery objects so you don't have to continually search for them like some sort of cache?

var cache = new Object();
cache['#object1'] = $('#object1');
cache['#object2'] = $('#object2');

To retrieve the jQuery values, you'd merely have to call cache['#object1'].

2 Comments

I'd recommend that you use the Object literal, {} to initialize an object rather than creating a new instance of Object. Eg. var cache = {};. This executes faster and saves a few bytes.
Thought it was the same. Thanks for the tip.
1

You can use .add():

var object1 = $('#object1'), object2 = $('#object2');

object1.add(object2).addClass('couple');

Or,

var multiple = [object1[0], object2[0], ...];

$(multiple).addClass('party');

Comments

0

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.

Comments

0

Why don't you use the easy way.

var severalVar = 'h1,h2,h3,h4,h5,h6';
 $(severalVar).hide()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.