an array of strings as a jQuery selector?

javascript, jquery

Solution

Well, there's 'join':

["#p1", "#p2", "#p3", "#p4", "#p5"].join(", ")

EDIT - Extra info:

It is possible to select an array of elements, problem is here you don't have the elements yet, just the selector strings. Any way you slice it you're gonna have to execute a search like .getElementById or use an actual jQuery select.

Problem

I have an array of strings that are valid jQuery selectors (i.e. IDs of elements on the page): ``` ["#p1", "#p2", "#p3", "#p4", "#p5"] ``` I want to select elements with those IDs into a jQuery array. This is probably elementary, but I can't find anything online. I could have a for-loop which creates a string `"#p1,#p2,#p3,#p4,#p5"` which could then be passed to jQuery as a single selector, but isn't there another way? Isn't there a way to pass an array of strings as a selector? EDIT: Actually, there is an answer out there already.

Original source

Related problems