> From: SLR
> 
> First off, I apologize if this is too "noobie" a question or 
> has been answered somewhere else (I can't find it anywhere). 
> I'm new to jQuery, and I'm trying to learn some basics. 

No question is too "noobie". Welcome aboard! :-)

> Anyways, I'm stumped on the following.
> 
> How can I convert this to jQuery?
> 
> var links = ...                                // Holds an array/
> collection of links
> function myfuction(param){...}        // Just a generic function that
> accepts a parameter
> 
> for(i=0; i< links.length; i++)
> {
>     links[i].onclick = myfuction(i); // note that the fuction 
> is being passed the loop control variable }
> 
> Basically, all I want to do is have the links pass their 
> "reference number" to a function.

Is myfunction supposed to be a click event handler itself, or is it a
function that returns another function which is the click handler? You're
calling myfunction immediately, so it can't be a click handler.

> So far, I got something like this:
> 
> $("a").each($(this), function(){
>       .click(function(){ ... });
> }
> 
> At this point, I have no idea where to go. Does the each 
> method have anything similar to a "loop control" variable? 
> I'm sure I can find a work around (ie: number assign each 
> link a number via the rel attribute) but I'd rather learn the
> correct way that trying to fake it.

That code won't work at all.

I would suggest reading the doc page on .each():

http://docs.jquery.com/Core/each#callback

But be advised that this page has several errors in it. (I'll clean it up
when I get some time if no one else gets to it first.)

If you just want the loop index, it's passed to the .each() callback as the
first parameter:

    $('a').each( function( i ){
        // 'i' is the loop index
        $(this).click(function(){
            // You can use 'i' directly in this code
        });
    });

But is the loop index that useful here? I'm trying to picture what you might
do with it. There may be a better way to do this - if you could say more
about your application, someone may have a suggestion.

-Mike

Reply via email to