7

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.

1
  • 2
    Your sample code is basically saying "find the #a element, then look for a #b element inside #a, then look for a #c inside #b, and a #d inside #c". Phrogz has the solution below. Commented Dec 13, 2011 at 17:56

4 Answers 4

17

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I used your .add.add solution because I find string to be not elegant. Thanks!
3
$("#a, #b, #c, #d").click();

It is called a comma :-D

Comments

2

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

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

Comments

1

do a comma

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

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.