[thelist] JavaScript - adding onclick events to links that do not execute (advanced)

Matt Warden mwarden at gmail.com
Fri Nov 25 13:28:27 CST 2005

On 11/25/05, Tom Dell'Aringa <pixelmech at yahoo.com> wrote:
> var filmstripItems = new Array();
>         filmstripItems[0] = "colorado";
>         filmstripItems[1] = "scene2";
>         filmstripItems[2] = "scene3";
>         for(var y=0; y<anchors.length; y++)
>         {
>                 anchors[y].onclick = function() {
>                         ajaxController('load_full', ""+filmstripItems[y]+"");
>                         return false;
>                 }
>                 anchors[y].href = "http://www.google.com/";
>         }
> ============================================
> I've tried every variation of ""+filmstripItems[y]+"" (quotes combinations) to get that array
> item to go as a string but I constantly get 'undefined'. If I alert() that guy, it shows me the
> value, but passing it seems to be the issue. Any ideas?

If you alert it from within the onclick, you won't get the value.

y has no value within that function. It only has meaningful values in
the context of your loop. The value doesn't get evaluated at the time
you assign the function. it gets evaluated when the function is
*executed*. Hope that makes sense.

Hopefully someone will pipe up with a better way to do this, but I
can't remember the better way at the moment. This should get you
rowking, though:

eval ("anchors["+y+"].onclick = function() {
                        ajaxController('load_full', '"+filmstripItems[y]+"');
                        return false;

Matt Warden
Miami University
Oxford, OH, USA

This email proudly and graciously contributes to entropy.

More information about the thelist mailing list