Help with simple javascript loop

Posted by Gabriel on Stack Overflow See other posts from Stack Overflow or by Gabriel
Published on 2011-02-16T06:33:01Z Indexed on 2011/02/16 7:25 UTC
Read the original article Hit count: 162

Filed under:
|
|

Hello,

I have a simple javascript that I'd like to loop for multiple elements. Here's my code:

<script type='text/javascript'>  
for(i = 1; i < 100; i++)
{
$('#link'+i).click(function() {
    $('#container').removeClass();
    $('#container').addClass('templateid'+i);
});
}
</script>

What I'd like to achieve is the same addClass function for multiple id's (e.g. link2, link3, link4), with the corresponding class (e.g. template2, template3, template4).

Any help would be hugely appreciated!

For reference, an individual call like this one, does work, so I don't see why the loop above doesn't function the same:

<script type='text/javascript'> 
$('#link2').click(function() {
    $('#container').removeClass();
    $('#container').addClass('templateid2');
});
</script> 

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery