How about changing the technique a bit like:

for (var i=0; i < TAGS.length; i++) {
    TAGS[i].data('count', i)
        .click(function(event) {
            alert( B[$(this).data('count')] );
        });
}

Basically it stores the value of 'i' with the element using .data(),
and you can retrieve it again later when the click is executed.

This might not work if the index of the element in TAGS changed. If $
(".class") is index-0, make sure no other element will take it's
index, otherwise you'll have to update the stored value using .data().

On Jun 30, 3:30 pm, Olivier <the.fuz...@gmail.com> wrote:
> Hi,
>
> I'm trying to to something in javascript but I don't know how to do
> that.
>
> Basicly, I have something like this :
> TAGS = new Array(
>         $(".class"),
>         $("#foo")
> );
>
> B = new Array(
>         "hey",
>         "oh"
> );
>
> for (var i=0; i < TAGS.length; i++) {
>         TAGS[i].click( function(event) {
>                 alert(B[i]);
>         });
>
> }
>
> I would like to alert "hey" when the user clicks on a tag of class
> "class" and to alert "oh" when he clicks on a tag of id "foo".
> This doesn't work because B[i] takes the "i" when the callback
> function is called by jquery (when a click occurs). So it always takes
> B[2] in this case (which doesn't exist).
> How could I effectively alert B[0] for .class tags and B[1] for #foo
> tags ?
>
> Thanks,
> Olivier

Reply via email to