I have an array of element identifiers, coming back from some server-side validation. The IDs aren't prefixed with a '#'. Rather than going through the array and prefixing a # to each member, is there jquery means of directly selecting all elements by their IDs?
In jQuery you can select by ID like this
$("[id=id_value]"); // returns 1 id
if you name them something like id_1 and id_2 you can do this
$("[id^='id_]") // returns multiple
If you have the ID as a string you can select it in jQuery like this
$("#"+id); //gives you one element
If you have multiple ID's that are similar, then use Elzo's suggestion.
You could just join them, like this:
var ids = ['div1', 'div2', 'div3'];
$('#' + ids.join(',#')).click(function() { alert('hi'); });
(NB - I haven't tried this - this is off the top of my head)
Let's say your array is "arr".
Couldn't you map your array of string identifiers into an array of jQuery objects, then concantenate them all using the usual jQuery selector?
$($.map(arr, function(id) { return $('#' + id); }))
Don't you forget "old fashioned" getElementById - it doesn't require hashing the ids. Then just feed nodes to jQuery to get a jQuery object:
var ids = ['jq-primarySearch', 'jq-n-CSS'];
var nodes = $.map( ids, function(i) { return document.getElementById(i) } );
var jqObj = $(nodes);
Just do the node selection yourself then wrap the result:
$(document.getElementById(id))
saves constructing a string selector that jQuery will only have to parse back in and then do exactly the same thing. Plus you don't have to worry about escaping characters like ‘:’ and ‘.’ which are valid in IDs but mean something else in selectors.