Bob the Builder
Bob the Builder

Reputation: 33

How to use multiple selectors when said selectors are stored in variables in jQuery?

I tried this but it's not working:

var a = $('a');
var b = $('p');
var c = $('div');

$(a, b, c).hide();

Upvotes: 3

Views: 121

Answers (3)

Jon Gauthier
Jon Gauthier

Reputation: 25582

jQuery selectors work like CSS selectors. You can select multiple independent sets of elements if you separate their selectors with commas:

$('a, p, div').hide();

If you already have fetched the different sets, you have the option of re-fetching them together (above), running .hide() on each one, or merging the sets to run .hide() once:

a.add(b).add(c).hide();

For more information see the .add() documentation.

Upvotes: 4

karim79
karim79

Reputation: 342635

Just to add, if you really do need to glue selection objects together, you can use .add:

a.add(b).add(c).hide();

Just to add (haha)

Upvotes: 1

Paul
Paul

Reputation: 141827

You'll have to use add():

a.add(b).add(c).hide();

Upvotes: 1

Related Questions