"but it definitely is not doing what I expect"

Start by understanding that the selector value is typically a *string*
value

Also, the "each" function passes an index along to the event, so with
those both said:

$(".CustomClass").each(function(idx){
        $(this).addClass("index_" + idx);
});

will do what you are after

also some other notes/tips:

<a> tags don't have a "src" attribute...

if you are uniquely trying to identify an object, use the "id" not a
unique class....


On Dec 1, 2:07 pm, yskel <josh.lucasf...@madmuseum.org> wrote:
> 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.

Reply via email to