Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate selector in jQuery?

Tags:

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.

like image 747
Bill Software Engineer Avatar asked Dec 13 '11 17:12

Bill Software Engineer


People also ask

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

How many types of jQuery selectors are there?

So far we have covered only three standard jQuery Selectors.

How to append string in jQuery?

The .append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend() ). The .append() and .appendTo() methods perform the same task. The major difference is in the syntax-specifically, in the placement of the content and target.


1 Answers

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);
like image 131
Phrogz Avatar answered Sep 23 '22 08:09

Phrogz