[jQuery] jQuery selector fails when in FOR loop
I have a set of <div>s that are created using jQuery. My script sets
an ID for each <div> and appends a counter variable to this ID. For
example, defName0, defName1, defName2, etc.
Within the same for loop that generates the IDs I would also like to
assign a jQuery function to the header. However, it seems that I
cannot select ANY jQuery objects from within a FOR loop.
In the sample code below I have hardcoded the ID (rather than using
"#defName"+[i] which doesn't work either). If I stick these two lines
in a FOR loop the function is not bound to the object and does not
fire on click (even though it doesn't reference the counter variable
used in the For loop). If I pull this out of the loop it works as
expected.
$("#defName0").bind("click", function() {
$("#defValue0").fadeIn("normal");
My questions are twofold:
1. Obviously, how do I get this to work in a For loop, if at all?
2. What is the proper syntax to add a counter variable to a jQuery
selector in a for loop? Is it "#defName"+[i], "defName+[i]", or what?
Thanks.