Reputation: 3896
I've been fiddling with this trying to use splice and join method but can't seem to get the effect that I really want.
I want to put a number sign (#) right before the strings. so I could use those array in targeting for jQuery. I can't edit the array and manually place the hastag because the array will come somewhere else.
my code is something like this:
$(function(){
var arr = ['item-1', 'item-2', 'item-3'];
// I want to convert it from 'item-1, item-2, item-3' to '#item-1, #item-2, #item-3'
$(arr).css({ 'border', 'red solid 1px'})
});
fiddle here: http://jsfiddle.net/6EkKe/
Upvotes: 0
Views: 31
Reputation: 4280
You do not need to necessarily edit the array, instead you can add to the values before you use them in Jquery.
Individual items
var arr = ['foo', 'bar', 'baz'];
$(arr).each(function(key, value)){
var id = '#' + value;
$(id).css({ 'border', 'red solid 1px'});
});
Using them all at the same time
var arr = ['foo', 'bar', 'baz'];
var ids = '#' + arr.join(', #');
$(ids).css({ 'border', 'red solid 1px'});
Explanation
Array.join() takes a parameter, this parameter is a string that will be put in between each of the array element, if left blank it will use ''
Upvotes: 1