Benjamin,

Thanks for the tip! I used the "this" variable when assigning the mouseover and mouseout events, but not here. I guess that's because I was referencing a unique ID and not just a certain class that would be on multiple elements. Maybe I don't understand the "this" variable like I thought I did.

I'll let you know what works out. I may holler with more questions, though.

Thanks!
Chris

Benjamin Sterling wrote:

Chris,

 

I would use the “this” variable in place of the i ie foo(this, ConstantArgument)  see if that helps.

 

Ben

 


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Christopher Jordan
Sent: Wednesday, October 25, 2006 4:15 PM
To: jQuery Discussion.
Subject: [jQuery] I need help with the jQuery .click event

 

Hi folks,

I've got a function that builds several elements on a page with a certain class. I've successfully used jQuery to add mouseover and mouseout events to all the elements with that certain class, and it works beautifully. I couldn't be happier. :0)

So, now I need to add a click event to each of those elements. The trouble is, that not each of those click events will be exactly the same. Each will call the same function but with slightly different arguments (one constant and one variable). After playing with a couple of ideas on how to use jQuery to accomplish this, I ended up with the following:

... inside a JS for loop with a counter 'i' ...


    $("##MyContainerID").append("<span id=\"id_" + i + "\" class=\"MyClass\" value=\"" + i + "\">" + i + "</span>");
    $("##id_" + i).click(
        function(){
            foo(i,ConstantArgument);
        }
    );

... end loop ...

The loop creates each of these elements on the page and I was hoping it would then assign a click event to each element (made unique with the id), and this seemed to work. However when I click on each of the elements in turn I can see that they are each calling the function (foo) with the same arguments (30,40). While 40 is the constant argument and is correct, the other argument was supposed to range from 1 - 29 (the number of elements in this particular list).

I'm a bit confused I thought this should work. Can anyone shed any light on what I may be doing wrong? Is there a different approach i need to take? Is there a better jQueryie-type way to accomplish this task that will make life easier?

Thanks,
Chris


_______________________________________________ jQuery mailing list [email protected] http://jquery.com/discuss/

No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.1.408 / Virus Database: 268.13.11/497 - Release Date: 10/25/2006

  
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to