The title plus the following example are self-explanatory of what I don't achieve :-) The idea is to replace something + counter in order to make it work.
for (var counter = 1; counter <= 6; counter++) {
var something + counter = $('element' + counter);
(something + counter).removeAttribute('class');
}
The title plus the following example are self-explanatory of what I don't achieve :-) The idea is to replace something + counter in order to make it work.
for (var counter = 1; counter <= 6; counter++) {
var something + counter = $('element' + counter);
(something + counter).removeAttribute('class');
}
Share
Improve this question
asked Aug 23, 2011 at 13:16
BrunoBruno
9,07713 gold badges40 silver badges55 bronze badges
2
- 1 Why not use an array, or apply those changes directly? – Sleeperson Commented Aug 23, 2011 at 13:19
-
1
Why do you need to create a variable in the loop at all? Can you not just do
$('element' + counter).removeAttribute('class');
? – James Allardice Commented Aug 23, 2011 at 13:19
5 Answers
Reset to default 3You could create an array, but much more simply:
for (var counter = 1; counter <= 6; counter++) {
$('element' + counter).removeAttribute('class');
}
Just do:
for (var counter = 1; counter <= 6; counter++) {
$('element' + counter).removeAttribute('class');
}
Unless you wanted to store it outside of the loop, in which case use an array.
Use an array.
var something = [];
for (var counter = 1; counter <= 6; counter++) {
something[counter] = $('element' + counter);
something[counter].removeAttribute('class');
}
Why can't you just get rid of the var altogether??
for (var counter = 1; counter <= 6; counter++) {
$('element' + counter).removeAttribute('class');
}
for (var counter = 1; counter <= 6; counter++) {
window[something + counter] = $('element' + counter);
window[something + counter].removeAttribute('class');
}
after that there will be a set of fields in window object, named something1, something2 etc (if something == "something"
, of course)