Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

an array of strings as a jQuery selector?

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.

like image 403
montrealist Avatar asked Jun 16 '09 15:06

montrealist


People also ask

How do you find if an array contains a specific string in jQuery?

inArray( value, array [, fromIndex ] )Returns: Number. Description: Search for a specified value within an array and return its index (or -1 if not found).

Does jQuery selector return an array?

returns an array of one DOM Element.


1 Answers

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.

like image 105
Paul Avatar answered Sep 30 '22 05:09

Paul