2

So I'm trying to input an array into a jquery selector and for some reason I can't get it to work at all. I've tried a few approaches below 1st:

var id = ["#id1", "#id2", "#id3"];
$(id[0]).css('color', '#000');

my other attempt:

var id = [];
for(var x = 0;x < 3; x++){
id[x] = x;
$("#id"+id[0]).css('color', '#000');
}

So I'm wondering what might be the approach to solving this?

example of what I'm using this for:

for(var x = 0;x < 28; x++){
function randomizerlet(){
randlet[x] = Math.floor(Math.random()*156);
timesletrun[x] += 1;
if (masterlet[randlet[x]] == letter[x]){
$('#flipL4_1').css('background-position', masterlet[randlet[x]]);
clearInterval(intervallet[x]);
}
else{
    if(timesletrun[x] == 300){
        masterlet[randlet[x]] = letter[x];
        $('#flipL4_1').css('background-position', masterlet[randlet[x]]);
        clearInterval(intervallet[x]);
        }
    else{
        $('#flipL4_1').css('background-position', masterlet[randlet[x]]);
        }}


        }
var intervallet[x] = setInterval(function() {
 randomizerlet();}, 10);
}
4
  • If you are trying to do different things to id's in the list then you can't be efficient. If you are setting everything in the lists color to black then Matt's answer along with his 'use a class' suggestion is ideal. Commented Apr 1, 2013 at 4:23
  • your first attempt works well, look: jsfiddle.net/MMSrD Commented Apr 1, 2013 at 4:29
  • Does function randomizerlet[x](){ even run? Commented Apr 1, 2013 at 4:34
  • no your right, that does not work, sorry. Commented Apr 1, 2013 at 4:59

1 Answer 1

5

Join the array elements into a single string:

var id = ["#id1", "#id2", "#id3"];
$(id.join(',')).css('color', '#000');

Or use a loop:

for (var i=0; i<id.length; i++) {
    $(id[i]).css(...);
}

Or a different kind of loop:

$.each(id, function (i, elt) {
    $(elt).css(...);
});

Or an even fancier sort of loop:

var $elements = $.map(id, function (elt, i) {
    return $(elt);
});
$elements.css(...);

All that said, it would probably be better to simply use a common class instead of IDs. Any time you think you need number-like indices, that's a strong hint that you might be better off using classes instead.

Sign up to request clarification or add additional context in comments.

5 Comments

I need to have them separated, not have all of them the same.
I do not understand. Please elaborate.
the $("#id1").css('color', '#000'); is inside a function whereas $("#id2").css('color', '#000'); will be in a different function cause they are recieving different inputs for .css('color', 'different variable').
You still haven't explained enough for me to do your thinking for you. I'm not a mind reader, so you're on your own for this one.
edited posting for a better understanding of what I need, the id needs to loop through as well, each id is not the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.