Tuesday, 17 September 2013

how do I properly incorporate arrays into for loops in jquery/javascript?

how do I properly incorporate arrays into for loops in jquery/javascript?

I am working on a custom gallery right now, but I cannot seem to get the
array of variables to apply during the loop. What am I missing?
Here is the relevant code:
var racerImage = ['$("img.ryanthumb")', '$("img.pierthumb")',
'$("img.jeremythumb")',
'$("img.mattthumb")', '$("img.andrewthumb")', '$("img.alanthumb")',
'$("img.kevinthumb")', '$("img.mikethumb")', '$("img.dougthumb")'];
var showMe= ['showRacer("ryan")\;', 'showRacer("pier")\;',
'showRacer("jeremy")\;', 'showRacer("matt")\;',
'showRacer("andrew")\;',
'showRacer("alan")\;', 'showRacer("kevin")\;', 'showRacer("mike")\;',
'showRacer("doug")\;'];
for (var i = 0; i < racerImage.length; i++)
{
racerImage[i].click(function(){
switch (i)
{
case 0:
showMe[i];
break;
case 1:
showMe[i];
break;
case 2:
showMe[i];
break;
case 3:
showMe[i];
break;
case 4:
showMe[i];
break;
case 5:
showMe[i];
break;
case 6:
showMe[i];
break;
case 7:
showMe[i];
break;
case 8:
showMe[i];
break;
}
});
}
Basically I am trying to use a for loop to apply the jquery multiple times
instead of writing it over and over again. I don't know if I am going
about this the right way, but any insights would be great. Thanks again!

No comments:

Post a Comment