Can you show what your for loop currently looks like? Answer for question #2: using the ".each" method, a paramenter is passed to the anonymous function that is the current 0-based index of the item in the collection
without seeing your code, i will take a guess at helping you though $("div[id^='defName']").bind("click", function() { var id = $(this).id.substr(7); $("#defValue" + id).fadeIn("normal"); }); On Dec 12, 3:25 pm, raskren <rask...@gmail.com> wrote: > 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.