It is not 100% clear why it isn't working for you. My first guess would be that you aren't quite building the URL string correctly. The URLs you are creating will look like this:
http://example.com/img/1.jpg
http://example.com/img/2.jpg
http://example.com/img/3.jpg
In addition, there's some funny business with your for
loop and the y
test in that loop. You can clean that up (in case that's causing an issue) by changing to this:
function go(/* args go here */) {
var icons = document.getElementsByClassName("icon");
for(var x = 0; x < arguments.length; x++)
icons[x].src = "http://example.com/img/" + arguments[x] + ".jpg";
}
Here are the changes and why:
- I removed the
y
part of the for
loop because y < (y + 8)
is always true so there's no point in having it as a test for the for
loop.
- I also never saw any place where you initialized
y
.
- You were using the comma operator in your condition, not the
&&
operator which also seemed wrong. Multiple conditions in the if
condition should use boolean operators, not the comma operator.
x
was a global. I changed it to a local variable since it is initialized and used locally and it's a dangerous name for a global.
- Changed to
icons[x]
since y
is no longer used.
- Moved the definition and initialization of
icons
into the function since I see no reason it needs to be a global either.
- Removed the definition of the arguments since the named arguments were not being used.
If, what you were trying to do is to make sure you never went past the last icon, then your loop can just be this:
function go(/* args go here */) {
var icons = document.getElementsByClassName("icon");
for (var x = 0; x < arguments.length && x < icons.length; x++)
icons[x].src = "http://example.com/img/" + arguments[x] + ".jpg";
}
Then, lastly if all you want to do is create the URLs as a sequence, you can just pass the begin and ending sequence numbers:
function go(begin, end) {
var icons = document.getElementsByClassName("icon");
for (var x = 0; x < icons.length && begin <= end; x++, begin++)
icons[x].src = "http://example.com/img/" + begin + ".jpg";
}
go(1, 8);