Thanks - that did it.  I knew I was making this a lot more difficult
than it needed to be.

I'd actually tried something syntactically similar to what you
suggested before, but it didn't work:

$(".CustomClass").each(function(i){
        $(this).addClass(i);
});

As you pointed out, however, this was trying to assign an int value to
the selector, whereas adding 'index_' to that int gives a string as a
result.  I'm really glad you pointed this out - I tried the above
solution first, but the failure there made me go further away from the
solution.  Thanks a lot.

And oops about the 'src' vs 'href' - typed 'a' but thought 'img.'

josh


On Dec 1, 2:55 pm, MorningZ <morni...@gmail.com> wrote:
> "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