2

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');
      }     
2
  • 1
    Why not use an array, or apply those changes directly? 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');? Commented Aug 23, 2011 at 13:19

5 Answers 5

3

You could create an array, but much more simply:

  for (var counter = 1; counter <= 6; counter++) {
    $('element' + counter).removeAttribute('class');
  } 
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

Use an array.

var something = [];
for (var counter = 1; counter <= 6; counter++) {
    something[counter] = $('element' + counter);
    something[counter].removeAttribute('class');
}

Comments

2

Why can't you just get rid of the var altogether??

for (var counter = 1; counter <= 6; counter++) {
    $('element' + counter).removeAttribute('class');
}

Comments

0
  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)

Comments

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.