Bill Software Engineer
Bill Software Engineer

Reputation: 7782

Concatenate selector in jQuery?

I am not sure if "concatenate" is the correct term for it, but something like this:

$("#a").$("#b").$("#c").$("#d").click();   // click on all of them

Basically I have a long list of stuff but I can't apply a class to them.

Upvotes: 7

Views: 15236

Answers (4)

Shyju
Shyju

Reputation: 218732

do a comma

$("#a,#b,#c,#d").click()

Upvotes: 1

Phrogz
Phrogz

Reputation: 303253

As in CSS, you can use a comma to separate multiple selectors:

$("#a, #b, #c, #d").click();

Note that these do not have to be the same kind of selector. For example:

// Click the menu, all spans in all .foo, and paragraphs after headers
$("#menu, div.foo span, h1 + p").click();

Also, if you already have the jQuery objects, you can add() the sets like so:

var a = $('#a'), b = $('#b'), c = $('#c');
var all = a.add(b).add(c);

Upvotes: 17

ShankarSangoli
ShankarSangoli

Reputation: 69905

You can concatenate selectors using comma(,) separator. Try this.

$("#a,#b,#c,#d").click();

Upvotes: 2

Naftali
Naftali

Reputation: 146310

$("#a, #b, #c, #d").click();

It is called a comma :-D

Upvotes: 3

Related Questions