New to jQuery, and I have a problem that I suspect has a really simple solution:
For a set of elements matching $(a.CustomClass), how do I add a class to each element based on that element's index? For instance my document contains: <a class='CustomClass' src='#'>test</a> <a class='CustomClass' src='#'>test</a> <a class='CustomClass' src='#'>test</a> I'd like to run the jQuery script to end up with something like this: <a class='CustomClass index_0' src='#'>test</a> <a class='CustomClass index_1' src='#'>test</a> <a class='CustomClass index_2' src='#'>test</a> I've been trying to use: $(a.CustomClass).each(function(){ var index = $(this).index(this); $(this).addClass(index); }); but it definitely is not doing what I expect. Any help would be greatly appreciated.